The R Project SVN R

Rev

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

Rev 68948 Rev 71228
Line 1... Line 1...
1
% File src/library/methods/man/NextMethod.Rd
1
% File src/library/methods/man/NextMethod.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-2011 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{callNextMethod}
6
\name{callNextMethod}
7
\alias{callNextMethod}
7
\alias{callNextMethod}
8
\title{Call an Inherited Method}
8
\title{Call an Inherited Method}
Line 89... Line 89...
89
}
89
}
90
 
90
 
91
\examples{
91
\examples{
92
 
92
 
93
## some class definitions with simple inheritance
93
## some class definitions with simple inheritance
94
setClass("B0" , representation(b0 = "numeric"))
94
setClass("B0", slots = c(b0 = "numeric"))
95
 
-
 
96
setClass("B1", representation(b1 = "character"), contains = "B0")
95
setClass("B1", slots = c(b1 = "character"), contains = "B0")
97
 
-
 
98
setClass("B2", representation(b2 = "logical"), contains = "B1")
96
setClass("B2", slots = c(b2 = "logical"), contains = "B1")
99
 
97
 
100
## and a rather silly function to illustrate callNextMethod
98
## and a rather silly function to illustrate callNextMethod
101
 
99
 
102
f <- function(x) class(x)
100
f <- function(x) class(x)
103
 
101
 
Line 138... Line 136...
138
removeGeneric("f")
136
removeGeneric("f")
139
 
137
 
140
removeMethods("Ops")
138
removeMethods("Ops")
141
 
139
 
142
## tests of multiple callNextMethod
140
## tests of multiple callNextMethod
143
setClass("m1", representation(count = "numeric"), contains = "matrix",
141
setClass("m1", slots = c(count = "numeric"), contains = "matrix",
144
         prototype = prototype(count = 0))
142
         prototype = prototype(count = 0))
145
mm1 <- new("m1", matrix(1:12, 3,4))
143
mm1 <- new("m1", matrix(1:12, 3,4))
146
setMethod("[", "m1", function(x, i, j, ..., drop) callNextMethod())
144
setMethod("[", "m1", function(x, i, j, ..., drop) callNextMethod())
147
 
145
 
148
setClass("m2", representation(sum = "numeric"), contains = "m1")
146
setClass("m2", slots = c(sum = "numeric"), contains = "m1")
149
 
147
 
150
setMethod("Ops", c("m1", "m1"), function(e1, e2) {
148
setMethod("Ops", c("m1", "m1"), function(e1, e2) {
151
    as(e1, "matrix") <- callNextMethod()
149
    as(e1, "matrix") <- callNextMethod()
152
    e1@count <- max(e1@count, e2@count)+1
150
    e1@count <- max(e1@count, e2@count)+1
153
    e1})
151
    e1})
154
 
152
 
155
mm2 <- new("m2", matrix(1:12, 3, 4), sum = sum(1:12))
153
mm2 <- new("m2", matrix(1:12, 3, 4), sum = sum(1:12))
156
 
154
 
157
stopifnot(identical(mm2[,2], 4:6))
155
stopifnot(identical(mm2[,2], 4:6))
158
 
156
 
159
setClass("m3", representation(rowtags = "character"),contains = "m2")
157
setClass("m3", slots = c(rowtags = "character"),contains = "m2")
160
 
158
 
161
setMethod("[", signature(x="m3", i = "character", j = "missing",
159
setMethod("[", signature(x="m3", i = "character", j = "missing",
162
                         drop = "missing"),
160
                         drop = "missing"),
163
          function(x, i,j, ..., drop) {
161
          function(x, i,j, ..., drop) {
164
              xx <- callNextMethod(x, match(i, x@rowtags),)
162
              xx <- callNextMethod(x, match(i, x@rowtags),)