The R Project SVN R

Rev

Rev 69636 | Rev 71366 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 69636 Rev 71228
Line 1... Line 1...
1
% File src/library/methods/man/setMethod.Rd
1
% File src/library/methods/man/setMethod.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2007 R Core Team
3
% Copyright 1995-2016 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{setMethod}
6
\name{setMethod}
7
\alias{setMethod}
7
\alias{setMethod}
8
\alias{removeMethod}
8
\alias{removeMethod}
Line 152... Line 152...
152
 \emph{Programming with Data}
152
 \emph{Programming with Data}
153
 Springer (For the original S4 version.)
153
 Springer (For the original S4 version.)
154
}
154
}
155
\examples{
155
\examples{
156
\dontshow{  require(stats)
156
\dontshow{  require(stats)
157
  setClass("track",
-
 
158
    representation(x="numeric", y = "numeric"))
157
  setClass("track", slots = c(x="numeric", y = "numeric"))
159
  setClass("trackCurve", representation("track",
158
  setClass("trackCurve", contains = "track", slots = c(smooth = "numeric"))
160
    smooth = "numeric"))
-
 
161
  setClass("trackMultiCurve", representation(x="numeric", y="matrix", smooth="matrix"),
159
  setClass("trackMultiCurve", slots = c(x="numeric", y="matrix", smooth="matrix"),
162
          prototype = list(x=numeric(), y=matrix(0,0,0), smooth=
160
          prototype = list(x=numeric(), y=matrix(0,0,0), smooth= matrix(0,0,0)))
163
  matrix(0,0,0)))
-
 
164
}
161
}
165
 
162
 
166
require(graphics)
163
require(graphics)
167
## methods for plotting track objects (see the example for \link{setClass})
164
## methods for plotting track objects (see the example for \link{setClass})
168
##
165
##
Line 221... Line 218...
221
## The method definition can include the arguments, but
218
## The method definition can include the arguments, but
222
## if they're omitted, class "missing" is assumed.
219
## if they're omitted, class "missing" is assumed.
223
 
220
 
224
setMethod("summary", "missing", function() "<No Object>")
221
setMethod("summary", "missing", function() "<No Object>")
225
 
222
 
226
\dontshow{
223
\dontshow{% <--------------------> all the following are "assertion" tests
227
 
224
 
228
stopifnot(identical(summary(), "<No Object>"))
225
stopifnot(identical(summary(), "<No Object>"))
229
 
226
 
230
removeMethods("summary")
227
removeMethods("summary")
231
 
228
 
Line 249... Line 246...
249
## group methods
246
## group methods
250
 
247
 
251
setMethod("Arith", c("track", "numeric"), function(e1, e2){e1@y <-
248
setMethod("Arith", c("track", "numeric"), function(e1, e2){e1@y <-
252
  callGeneric(e1@y , e2); e1})
249
  callGeneric(e1@y , e2); e1})
253
 
250
 
254
 
-
 
255
t1  - 100.
251
t1  - 100.
256
 
-
 
257
t1/2
252
t1/2
258
 
253
 
259
 
-
 
260
## check it hasn't screwed up S3 methods
254
## check it hasn't screwed up S3 methods
261
ISOdate(1990, 12, 1)- ISOdate(1980, 12, 1)
255
ISOdate(1990, 12, 1)- ISOdate(1980, 12, 1)
262
 
256
 
263
## test the .Generic mechanism
257
## test the .Generic mechanism
264
 
258
 
Line 320... Line 314...
320
 
314
 
321
removeMethods("length")
315
removeMethods("length")
322
 
316
 
323
## methods for \%*\%, which isn't done by the same C code as other ops
317
## methods for \%*\%, which isn't done by the same C code as other ops
324
 
318
 
325
setClass("foo", representation(m="matrix"))
319
setClass("foo", slots = c(m="matrix"))
326
m1 <- matrix(1:12,3,4)
320
m1 <- matrix(1:12,3,4)
327
f1 = new("foo", m=m1)
321
f1 = new("foo", m=m1)
328
f2 = new("foo", m=t(m1))
322
f2 = new("foo", m=t(m1))
329
 
323
 
330
setMethod("\%*\%", c("foo", "foo"),
324
setMethod("\%*\%", c("foo", "foo"),
331
 function(x,y)callGeneric(x@m, y@m))
325
          function(x,y) callGeneric(x@m, y@m))
332
 
326
 
333
stopifnot(identical(f1\%*\%f2, m1\%*\% t(m1)))
327
stopifnot(identical(f1\%*\%f2, m1\%*\% t(m1)))
334
 
328
 
335
removeMethods("\%*\%")
329
removeMethods("\%*\%")
336
 
-
 
337
removeMethods("plot")
330
removeMethods("plot")
338
 
331
 
-
 
332
if(FALSE) ## Hold until removeMethods revised:
339
## Hold until removeMethods revised: stopifnot(existsFunction("plot", FALSE) && !isGeneric("plot", 1))
333
  stopifnot(existsFunction("plot", FALSE) && !isGeneric("plot", 1))
340
 
334
 
341
## methods for plotData
335
## methods for plotData
342
plotData <- function(x, y, ...) plot(x, y, ...)
336
plotData <- function(x, y, ...) plot(x, y, ...)
343
 
337
 
344
setGeneric("plotData")
338
setGeneric("plotData")
345
 
339
 
346
setMethod("plotData", signature(x="track", y="missing"),
340
setMethod("plotData", signature(x="track", y="missing"),
347
  function(x,  y, ...) plot(slot(x, "x"), slot(x, "y"), ...)
341
          function(x,  y, ...) plot(slot(x, "x"), slot(x, "y"), ...))
348
)
342
 
349
## and now remove the whole generic
343
## and now remove the whole generic
350
removeGeneric("plotData")
344
removeGeneric("plotData")
351
 
345
 
352
stopifnot(!exists("plotData", 1))
346
stopifnot(!exists("plotData", 1))
353
 
347
 
354
##  Tests of method inheritance & multiple dispatch
348
##  Tests of method inheritance & multiple dispatch
355
setClass("A0", representation(a0 = "numeric"))
349
setClass("A0", slots = c(a0 = "numeric"))
-
 
350
setClass("A1", contains = "A0", slots = c(a1 = "character"))
356
 
351
 
357
setClass("A1", representation("A0", a1 = "character"))
352
setClass("B0", slots = c(b0 = "numeric"))
-
 
353
setClass("B1", "B0") # (meaning 'contains = *')
-
 
354
setClass("B2", contains = "B1", slots = c(b2 = "logical"))
358
 
355
 
359
setClass("B0" ,representation(b0 = "numeric"))
-
 
360
 
-
 
361
setClass("B1", "B0")
-
 
362
 
-
 
363
setClass("B2", representation("B1", b2 = "logical"))
-
 
364
 
-
 
365
setClass("AB0", representation("A1", "B2", ab0 = "matrix"))
356
setClass("AB0", contains = c("A1", "B2"), slots = c(ab0 = "matrix"))
366
 
357
 
367
f1 <- function(x,  y)"ANY"
358
f1 <- function(x,  y)"ANY"
368
 
359
 
369
setGeneric("f1")
360
setGeneric("f1")
370
 
361
 
Line 406... Line 397...
406
})
397
})
407
 
398
 
408
setMethod("doubleAnything", "ANY", function(x)x)
399
setMethod("doubleAnything", "ANY", function(x)x)
409
 
400
 
410
setMethod("doubleAnything", "character",
401
setMethod("doubleAnything", "character",
411
function(x)paste("<",x,">",sep=""))
402
          function(x) paste("<",x,">",sep=""))
412
 
403
 
413
mustEqual(doubleAnything(1:10), c(1:10, 1:10))
404
mustEqual(doubleAnything(1:10), c(1:10, 1:10))
414
mustEqual(doubleAnything("junk"), rep("<junk>",2))
405
mustEqual(doubleAnything("junk"), rep("<junk>",2))
415
 
406
 
416
removeGeneric("doubleAnything")
407
removeGeneric("doubleAnything")
417
 
-
 
418
 
-
 
419
}
408
}% dontshow the above
420
}
409
}
421
 
410
 
422
\seealso{
411
\seealso{
423
 
412
 
424
\code{\link{method.skeleton}}, which is the recommended way to generate a skeleton of the call to \code{setMethod}, with the correct formal arguments and other details.
413
\code{\link{method.skeleton}}, which is the recommended way to generate a skeleton of the call to \code{setMethod}, with the correct formal arguments and other details.