### Name: setMethod ### Title: Create and Save a Method ### Aliases: setMethod removeMethod ### Keywords: programming classes methods ### ** Examples ## Don't show: require(stats) setClass("track", representation(x="numeric", y = "numeric")) setClass("trackCurve", representation("track", smooth = "numeric")) setClass("trackMultiCurve", representation(x="numeric", y="matrix", smooth="matrix"), prototype = list(x=numeric(), y=matrix(0,0,0), smooth= matrix(0,0,0))) ## End Don't show require(graphics) ## methods for plotting track objects (see the example for setClass) ## ## First, with only one object as argument: setMethod("plot", signature(x="track", y="missing"), function(x, y, ...) plot(slot(x, "x"), slot(x, "y"), ...) ) ## Second, plot the data from the track on the y-axis against anything ## as the x data. setMethod("plot", signature(y = "track"), function(x, y, ...) plot(x, slot(y, "y"), ...) ) ## and similarly with the track on the x-axis (using the short form of ## specification for signatures) setMethod("plot", "track", function(x, y, ...) plot(slot(x, "y"), y, ...) ) t1 <- new("track", x=1:20, y=(1:20)^2) tc1 <- new("trackCurve", t1) slot(tc1, "smooth") <- smooth.spline(slot(tc1, "x"), slot(tc1, "y"))$y #$ plot(t1) plot(qnorm(ppoints(20)), t1) ## An example of inherited methods, and of conforming method arguments ## (note the dotCurve argument in the method, which will be pulled out ## of ... in the generic. setMethod("plot", c("trackCurve", "missing"), function(x, y, dotCurve = FALSE, ...) { plot(as(x, "track")) if(length(slot(x, "smooth") > 0)) lines(slot(x, "x"), slot(x, "smooth"), lty = if(dotCurve) 2 else 1) } ) ## the plot of tc1 alone has an added curve; other uses of tc1 ## are treated as if it were a "track" object. plot(tc1, dotCurve = TRUE) plot(qnorm(ppoints(20)), tc1) ## defining methods for a special function. ## Although "[" and "length" are not ordinary functions ## methods can be defined for them. setMethod("[", "track", function(x, i, j, ..., drop) { x@x <- x@x[i]; x@y <- x@y[i] x }) plot(t1[1:15]) setMethod("length", "track", function(x)length(x@y)) length(t1) ## methods can be defined for missing arguments as well setGeneric("summary") ## make the function into a generic ## A method for summary() ## The method definition can include the arguments, but ## if they're omitted, class "missing" is assumed. setMethod("summary", "missing", function() "") ## Don't show: stopifnot(identical(summary(), "")) removeMethods("summary") ## for the primitives ## inherited methods length(tc1) tc1[-1] ## make sure old-style methods still work. t11 <- t1[1:15] identical(t1@y[1:15], t11@y) ## S3 methods, with nextMethod form <- y ~ x form[1] ## S3 arithmetic methods ISOdate(1990, 12, 1)- ISOdate(1980, 12, 1) ## group methods setMethod("Arith", c("track", "numeric"), function(e1, e2){e1@y <- callGeneric(e1@y , e2); e1}) t1 - 100. t1/2 ## check it hasn't screwed up S3 methods ISOdate(1990, 12, 1)- ISOdate(1980, 12, 1) ## test the .Generic mechanism setMethod("Compare", signature("track", "track"), function(e1,e2) { switch(.Generic, "==" = e1@y == e2@y, NA) }) #stopifnot(all(t1==t1)) #stopifnot(identical(t1",sep="")) mustEqual(doubleAnything(1:10), c(1:10, 1:10)) mustEqual(doubleAnything("junk"), rep("",2)) removeGeneric("doubleAnything") ## End Don't show