name
a valid generic function on library
where
.
If
name
is already a generic function on
where
,
setGeneric
modifies
def
or other
things, according to this call.
setGeneric(name, def, group, valueClass, where)
standardGeneric(name)
.
valueClass
slot for the generic.
1
.
GENERIC
containing the name of the
generic.
If the generic currently does not exist on
where
,
setGeneric
prepares an initial methods definition and stores
it. The default and usual case is that the function of the same name exists,
and it is used as a template to infer the arguments to the generic
and as a default method. This function can also be used to change the
generic function definition, for example, to or from a
standardGeneric
.
The ordinary object
name
on
where
after this task
is always the generic function definition, so that the
apply
family of functions will work.
There are two types of generic function dispatching systems in S-PLUS, "S3" and "S4" styles. They may be used together, as in the example below.
Use caution when defining S4 methods; they can be used when you don't expect it, with the input changed. In one example below, if a "vector" method is defined and a "matrix" method is not defined, then the "vector" method is used even for a matrix (because it is possible to turn a matrix into a vector by dropping the dimension information). Furthermore, the input is changed from a matrix to a vector.
# Define an S4 generic setGeneric("myDim", function(object) standardGeneric("myDim")) # Define S4 methods setMethod("myDim", "matrix", function(object) dim(object)) setMethod("myDim", "vector", function(object) length(object)) # Set the default S4 method to call UseMethod; this # makes it an S3 generic, so that S3 methods may be used. setDefaultMethod("myDim", function(object) UseMethod("myDim")) # S3 methods use this naming convention: # functionName.className # Define the S3 default myDim.default <- function(object) dim(object) # Define an S3 method myDim.data.frame <- function(object) length(object) # S4 methods are used first; if no S4 method applies, then the # S4 default is called. If the S4 default is an S3 generic # (i.e. if the S4 default calls UseMethod) # then S3 methods are used next. # Try those myDim(diag(3)) # uses the S4 matrix method myDim(fuel.frame) # uses the S3 data.frame method fit <- lm(Fuel ~ Weight, data = fuel.frame) myDim(fit) # uses the S3 default # Caution when using S4 methods; they may be used when you # don't expect it, and the input may be changed: removeMethod("myDim", "matrix") setMethod("myDim", "vector", function(object) { cat("You entered the vector method\n") cat("The input is:\n") object }) m <- matrix(1:4, 2) # a matrix myDim(m) # That produces this output: # You entered the vector method # The input is: # [1] 1 2 3 4 # Also note that S4 methods are used first; if an S4 method is # found which can be used (even if it requires changing the input) # then the S3 methods are ignored. # Clean up rm(myDim.data.frame, myDim.default) removeGeneric("myDim") # remove all remaining methods & generic