| Line 7... |
Line 7... |
| 7 |
methods can then be defined. Typically, an existing non-generic
|
7 |
methods can then be defined. Typically, an existing non-generic
|
| 8 |
function becomes the default method, but there is much optional
|
8 |
function becomes the default method, but there is much optional
|
| 9 |
control. See the details section.
|
9 |
control. See the details section.
|
| 10 |
}
|
10 |
}
|
| 11 |
\usage{
|
11 |
\usage{
|
| 12 |
setGeneric(name, def, group=NULL, valueClass=NULL, where=1, package,
|
12 |
setGeneric(name, def= , group=list(), valueClass=character(), where= ,
|
| 13 |
signature, useAsDefault, genericFunction)
|
13 |
package= , signature= , useAsDefault= , genericFunction= )
|
| 14 |
|
14 |
|
| 15 |
setGroupGeneric(name, def, group=NULL, valueClass=NULL, knownMembers,
|
15 |
setGroupGeneric(name, def= , group=list(), valueClass=character(),
|
| 16 |
package, where=1)
|
16 |
knownMembers=list(), package= , where= )
|
| 17 |
}
|
17 |
}
|
| 18 |
\arguments{
|
18 |
\arguments{
|
| 19 |
\item{name}{ The character string name of the generic function. In
|
19 |
\item{name}{ The character string name of the generic function. In
|
| 20 |
the simplest and most common case, a function of this name is
|
20 |
the simplest and most common case, a function of this name is
|
| 21 |
already defined. The existing function may be non-generic or
|
21 |
already defined. The existing function may be non-generic or
|
| Line 26... |
Line 26... |
| 26 |
no current function of this name. In that case, the formal arguments
|
26 |
no current function of this name. In that case, the formal arguments
|
| 27 |
and default values for the generic are taken from \code{def}. You
|
27 |
and default values for the generic are taken from \code{def}. You
|
| 28 |
can also supply this argument if you want the generic function to
|
28 |
can also supply this argument if you want the generic function to
|
| 29 |
do something other than just dispatch methods (an advanced topic
|
29 |
do something other than just dispatch methods (an advanced topic
|
| 30 |
best left alone unless you are sure you want it).
|
30 |
best left alone unless you are sure you want it).
|
| 31 |
|
31 |
|
| 32 |
Note that \code{def} is \emph{not} the default method; use argument
|
32 |
Note that \code{def} is \emph{not} the default method; use argument
|
| 33 |
\code{useAsDefault} if you want to specify the default separately.
|
33 |
\code{useAsDefault} if you want to specify the default separately.
|
| 34 |
}
|
34 |
}
|
| 35 |
\item{group}{ Optionally, a character string giving the group of
|
35 |
\item{group}{ Optionally, a character string giving the group of
|
| 36 |
generic functions to which this function belongs. Methods can be
|
36 |
generic functions to which this function belongs. Methods can be
|
| 37 |
defined for the corresponding group generic, and these will then
|
37 |
defined for the corresponding group generic, and these will then
|
| 38 |
define methods for this specific generic function, if no method has
|
38 |
define methods for this specific generic function, if no method has
|
| 39 |
been explicitly defined for the corresponding signature. See the
|
39 |
been explicitly defined for the corresponding signature. See the
|
| 40 |
references for more discussion.
|
40 |
references for more discussion.
|
| 41 |
}
|
41 |
}
|
| 42 |
\item{valueClass}{ An optional character vector or unevaluated
|
42 |
\item{valueClass}{ An optional character vector or unevaluated
|
| 43 |
expression. The value returned by the generic function must
|
43 |
expression. The value returned by the generic function must
|
| 44 |
have (or extend) this class, or one of the classes; otherwise,
|
44 |
have (or extend) this class, or one of the classes; otherwise,
|
| 45 |
an error is generated. See the
|
45 |
an error is generated. See the
|
| 46 |
details section for supplying an expression.
|
46 |
details section for supplying an expression.
|
| 47 |
}
|
47 |
}
|
| Line 132... |
Line 132... |
| 132 |
(though perhaps not a good idea, in that it makes the behavior of
|
132 |
(though perhaps not a good idea, in that it makes the behavior of
|
| 133 |
your function different from the usual S model). If your explicit
|
133 |
your function different from the usual S model). If your explicit
|
| 134 |
definition of the generic function
|
134 |
definition of the generic function
|
| 135 |
does \emph{not} call \code{standardGeneric} you are in trouble,
|
135 |
does \emph{not} call \code{standardGeneric} you are in trouble,
|
| 136 |
because none of the methods for the function will ever be
|
136 |
because none of the methods for the function will ever be
|
| 137 |
dispatched.
|
137 |
dispatched.
|
| 138 |
|
138 |
|
| 139 |
By default, the generic function can return any object.
|
139 |
By default, the generic function can return any object.
|
| 140 |
If \code{valueClass} is supplied, it should be a vector of
|
140 |
If \code{valueClass} is supplied, it should be a vector of
|
| 141 |
class names; the value returned by a method is then required to
|
141 |
class names; the value returned by a method is then required to
|
| 142 |
satisfy \code{is(object, Class)} for one of the specified classes.
|
142 |
satisfy \code{is(object, Class)} for one of the specified classes.
|
| Line 171... |
Line 171... |
| 171 |
"Compare", "Ops", "Math", "Math2", "Summary",} and \code{"Complex"})
|
171 |
"Compare", "Ops", "Math", "Math2", "Summary",} and \code{"Complex"})
|
| 172 |
does the same for each of the functions in that group.
|
172 |
does the same for each of the functions in that group.
|
| 173 |
|
173 |
|
| 174 |
You usually only need to use either function if the methods are
|
174 |
You usually only need to use either function if the methods are
|
| 175 |
being defined only for the group generic. Defining a method for a
|
175 |
being defined only for the group generic. Defining a method for a
|
| 176 |
primitive function, say \code{"+"}, by a call to \code{setMethod}
|
176 |
primitive function, say \code{"+"}, by a call to \code{setMethod}
|
| 177 |
turns on method dispatch for that function. But in R defining a
|
177 |
turns on method dispatch for that function. But in R defining a
|
| 178 |
method for the corresponding group generic, \code{"Arith"}, does not
|
178 |
method for the corresponding group generic, \code{"Arith"}, does not
|
| 179 |
currently turn on method dispatch (for efficiency reasons). If
|
179 |
currently turn on method dispatch (for efficiency reasons). If
|
| 180 |
there are no non-group methods for the functions, you have two
|
180 |
there are no non-group methods for the functions, you have two
|
| 181 |
choices.
|
181 |
choices.
|
| Line 200... |
Line 200... |
| 200 |
While the programming interface for the \pkg{methods} package follows
|
200 |
While the programming interface for the \pkg{methods} package follows
|
| 201 |
the reference, the R software is an original implementation, so
|
201 |
the reference, the R software is an original implementation, so
|
| 202 |
details in the reference that reflect the S4 implementation may appear
|
202 |
details in the reference that reflect the S4 implementation may appear
|
| 203 |
differently in R. Also, there are extensions to the programming
|
203 |
differently in R. Also, there are extensions to the programming
|
| 204 |
interface developed more recently than the reference. For a
|
204 |
interface developed more recently than the reference. For a
|
| 205 |
discussion of details and ongoing development, see the web page
|
205 |
discussion of details and ongoing development, see the web page
|
| 206 |
\url{http://developer.r-project.org/methodsPackage.html} and the
|
206 |
\url{http://developer.r-project.org/methodsPackage.html} and the
|
| 207 |
pointers from that page.
|
207 |
pointers from that page.
|
| 208 |
}
|
208 |
}
|
| 209 |
\examples{
|
209 |
\examples{
|
| 210 |
\dontshow{
|
210 |
\dontshow{
|
| Line 214... |
Line 214... |
| 214 |
|
214 |
|
| 215 |
### A non-standard generic function. It insists that the methods
|
215 |
### A non-standard generic function. It insists that the methods
|
| 216 |
### return a non-empty character vector (a stronger requirement than
|
216 |
### return a non-empty character vector (a stronger requirement than
|
| 217 |
### valueClass = "character" in the call to setGeneric)
|
217 |
### valueClass = "character" in the call to setGeneric)
|
| 218 |
|
218 |
|
| 219 |
setGeneric("authorNames",
|
219 |
setGeneric("authorNames",
|
| 220 |
function(text) {
|
220 |
function(text) {
|
| 221 |
value <- standardGeneric("authorNames")
|
221 |
value <- standardGeneric("authorNames")
|
| 222 |
if(!(is(value, "character") && any(nchar(value)>0)))
|
222 |
if(!(is(value, "character") && any(nchar(value)>0)))
|
| 223 |
stop("authorNames methods must return non-empty strings")
|
223 |
stop("authorNames methods must return non-empty strings")
|
| 224 |
value
|
224 |
value
|
| Line 242... |
Line 242... |
| 242 |
## An example of group generic methods, using the class
|
242 |
## An example of group generic methods, using the class
|
| 243 |
## "track"; see the documentation of setClass for its definition
|
243 |
## "track"; see the documentation of setClass for its definition
|
| 244 |
|
244 |
|
| 245 |
#define a method for the Arith group
|
245 |
#define a method for the Arith group
|
| 246 |
|
246 |
|
| 247 |
setMethod("Arith", c("track", "numeric"),
|
247 |
setMethod("Arith", c("track", "numeric"),
|
| 248 |
function(e1, e2){
|
248 |
function(e1, e2){
|
| 249 |
e1@y <- callGeneric(e1@y , e2)
|
249 |
e1@y <- callGeneric(e1@y , e2)
|
| 250 |
e1
|
250 |
e1
|
| 251 |
})
|
251 |
})
|
| 252 |
|
252 |
|
| 253 |
setMethod("Arith", c("numeric", "track"),
|
253 |
setMethod("Arith", c("numeric", "track"),
|
| 254 |
function(e1, e2){
|
254 |
function(e1, e2){
|
| 255 |
e2@y <- callGeneric(e1, e2@y)
|
255 |
e2@y <- callGeneric(e1, e2@y)
|
| 256 |
e2
|
256 |
e2
|
| 257 |
})
|
257 |
})
|
| 258 |
|
258 |
|