selectMethod() returns the method that
would be selected for a call to function
f if the arguments had
classes as specified by
signature. Failing to find a method
is an error, unless argument
optional = TRUE, in which case
NULL is returned.
findMethod() returns a list of
environments that contain a method for the specified function and signature; by
default, these are a subset of the packages in the current search
list. See section “Using
findMethod()” for details.
getMethod() returns the method corresponding to the
function and signature supplied similarly to
without using inheritance or group generics.
existsMethod() test whether
getMethod(), respectively, finds a matching method.
1 2 3 4 5 6 7 8 9 10 11
selectMethod(f, signature, optional = FALSE, useInherited =, mlist = , fdef = , verbose = , doCache = ) findMethod(f, signature, where) getMethod(f, signature = character(), where, optional = FALSE, mlist, fdef) existsMethod(f, signature = character(), where) hasMethod(f, signature = character(), where)
a generic function or the character-string name of one.
the signature of classes to match to the arguments
the environment in which to look for the
method(s). By default, if the call comes from the command line, the table of methods defined in the generic
function itself is used, except for
if the selection in
signature argument specifies classes, corresponding to
formal arguments of the generic function; to be precise, to the
signature slot 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_Details 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. 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
getMethod will normally use the
current version of the generic function in the R session, which has a
table of the methods obtained from all the packages loaded in the
session. Optional arguments can cause a search for the generic function from a
specified environment, but this is rarely a useful idea. In contrast,
findMethod has a different default and the optional
where= argument may be needed. See the section “Using
FALSE according to whether a method is found,
the first corresponding to
getMethod (no inheritance) and the
The call to
getMethod returns 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
NULL is returned if
The returned method object is a
MethodDefinition object, 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
The returned value of
findMethod is a list of
environments in which a corresponding method was found; that is, a
table of methods including the one specified.
As its name suggests, this function is intended to behave like
find, which produces a list of the packages on the
current search list which have, and have exported, the object named.
findMethod does also, by default. The
“exported” part in this case means that the package's namespace
exportMethods directive for this generic function.
An important distinction is that the absence of such a directive does not prevent methods from the package from being called once the package is loaded. Otherwise, the code in the package could not use un-exported methods.
So, if your question is whether loading package
thisPkg will define a
method for this function and signature, you need to ask that question
about the namespace of the package:
findMethod(f, signature, where = asNamespace("thisPkg"))
If the package did not export the method, attaching it and calling
findMethod with no
where argument will not find the
Notice also that the length of the signature must be what the
corresponding package used. If
thisPkg had only methods for
one argument, only length-1 signatures will match (no trailing
"ANY"), even if another currently loaded package had signatures
with more arguments.
setAs allows packages to define methods for
coercing one class of objects to another class. This works internally
by defining methods for the generic function
which can not be called directly.
The R evaluator selects
methods for this purpose using a different form of inheritance. While
methods can be inherited for the object being coerced, they cannot
inherit for the target class, since the result would not be a valid
object from that class.
If you want to
examine the selection procedure, you must supply the optional argument
useInherited = c(TRUE, FALSE) to
Chambers, John M. (2016) Extending R, Chapman & Hall. (Chapters 9 and 10.)
Chambers, John M. (2008) Software for Data Analysis: Programming with R Springer. (Section 10.6 for some details of method selection.)
Methods_Details for the details of method
GenericFunctions for other functions
manipulating methods and generic function objects;
MethodDefinition for the class that represents
1 2 3 4 5 6 7 8 9 10 11 12 13