selectMethodreturn the method; the functions
hasMethodtest for its existence. In both cases the first function only gets direct definitions and the second uses inheritance. In all cases, the search is in the generic function itself or in the package/environment specified by argument
findMethod returns the package(s) in the search
list (or in the packages specified by the
where argument) that
contain a method for this function and signature.
getMethod(f, signature=character(), where, optional = FALSE, mlist, fdef)existsMethod(f, signature = character(), where)findMethod(f, signature, where)selectMethod(f, signature, optional = FALSE, useInherited =, mlist = , fdef = , verbose = , doCache = )hasMethod(f, signature=character(), where)
f. See the details below.
selectMethoddoes not find a valid method an error is generated, unless this argument is
TRUE. In that case, the value returned is
NULLif no method matches.
selectMethodfor internal use. Avoid these: some will work as expected and others will not, and none of them is required for normal use of the functions.
getMethodreturns the selected method, if one is found. (This class extends
function, so you can use the result directly as a function if that is what you want.) Otherwise an error is thrown if
NULLis returned if
TRUE.The returned method object is a
MethodDefinitionobject, except that the default method for a primitive function is required to be the primitive itself. Note therefore that the only reliable test that the search failed is
signatureargument specifies classes, corresponding to formal arguments of the generic function; to be precise, to the
signatureslot of the generic function object. The argument may be a vector of strings identifying classes, and may be named or not. Names, if supplied, match the names of those formal arguments included in the signature of the generic. That signature is normally all the arguments except .... However, generic functions can be specified with only a subset of the arguments permitted, or with the signature taking the arguments in a different order.
It's a good idea to name the arguments in the signature to avoid
confusion, if you're dealing with a generic that does something
special with its signature. In any case, the elements of the
signature are matched to the formal signature by the same rules
used in matching arguments in function calls (see
The strings in the signature may be class names,
"ANY". See Methods for the meaning of these in
method selection. Arguments not supplied in the signature
implicitly correspond to class
"ANY"; in particular, giving
an empty signature means to look for the default method.
A call to
getMethod returns the method for a particular
function and signature. As with other
where controls where the function looks (by default
anywhere in the search list) and argument
whether the function returns
NULL or generates an error if
the method is not found. The search for the method makes no use of
selectMethod also looks for a method given the
function and signature, but makes full use of the method dispatch
mechanism; i.e., inherited methods and group generics are taken into
account just as they would be in dispatching a method for the
corresponding signature, with the one exception that conditional
inheritance is not used. Like
NULL or generates an error if
the method is not found, depending on the argument
FALSE according to whether a method is found,
the first corresponding to
getMethod (no inheritance) and the
Chambers, John M. (1998) Programming with Data Springer (For the original S4 version.)
Methodsfor the details of method selection;
GenericFunctionsfor other functions manipulating methods and generic function objects;
MethodDefinitionfor the class that represents method definitions.
setGeneric("testFun", function(x)standardGeneric("testFun")) setMethod("testFun", "numeric", function(x)x+1) hasMethod("testFun", "numeric") ## Not run:  TRUE hasMethod("testFun", "integer") #inherited ## Not run:  TRUE existsMethod("testFun", "integer") ## Not run:  FALSE hasMethod("testFun") # default method ## Not run:  FALSE hasMethod("testFun", "ANY") ## Not run:  FALSE
Run the code above in your browser using DataCamp Workspace