| methods {utils} | R Documentation |
List Methods for S3 Generic Functions or Classes
Description
List all available methods for a S3 and S4 generic function, or allmethods for an S3 or S4 class.
Usage
methods(generic.function, class, all.names = FALSE, dropPath = FALSE).S3methods(generic.function, class, envir = parent.frame(), all.names = FALSE, dropPath = FALSE, useEnv = FALSE)## S3 method for class 'MethodsFunction'format(x, byclass = attr(x, "byclass"), ...)## S3 method for class 'MethodsFunction'print(x, byclass = attr(x, "byclass"), ...)Arguments
generic.function | a generic function, or a character string naming ageneric function. |
class | a symbol or character string naming a class: only used if |
envir | the environment in which to look for the definition ofthe generic function, when the generic function is passed as acharacter string. |
all.names | a |
dropPath | a |
useEnv | a |
x | typically the result of |
byclass | an optional |
... | potentially further arguments passed to and from methods;unused currently. |
Details
methods() finds S3 and S4 methods associated with either thegeneric.function orclass argument.Methods found are those provided by all loaded namespaces viaregistration, seeUseMethod; normally, this includesall packages on the currentsearch() path..S3methods()finds only S3 methods,.S4methods() finds only S4 methods.
When invoked with thegeneric.function argument, the"byclass" attribute (see Details) isFALSE, and theprint method by default displays the signatures (full names) ofS3 and S4 methods. S3 methods are printed by pasting the genericfunction and class together, separated by a ‘.’, asgeneric.class. The S3 method name is followed by an asterisk* if the method definition is not exported from the packagenamespace in which the method is defined. S4 method signatures areprinted asgeneric,class-method; S4 allows for multipledispatch, so there may be several classes in the signaturegeneric,A,B-method.
When invoked with theclass argument,"byclass" isTRUE, and theprint method by default displays the namesof the generic functions associated with the class,generic.
The source code for all functions is available. For S3 functionsexported from the namespace, enter the method at the command line asgeneric.class. For S3 functions not exported from thenamespace, seegetAnywhere orgetS3method. For S4methods, seegetMethod.
Help is available for each method, in addition to each generic. Forinteractive help, use the documentation shortcut? with thename of the generic and tab completion,?"generic<tab>" toselect the method for which help is desired.
The S3 functions listed are those whichare named like methodsand may not actually be methods (known exceptions are discarded in thecode).
Value
An object of class"MethodsFunction", a character vector ofmethod names with"byclass" and"info" attributes. The"byclass" attribute is alogical indicating ifthe results were obtained with argumentclassdefined. The"info" attribute is a data frame with columns:
- generic
charactervector of the names of the generic.- visible
logical(), is the method “visible” to the user?When true, it typically is exported from the namespace of the packagein which it is defined, and the package is
attach()edto thesearch()path.- isS4
logical(), true when the method is an S4 method.
- from
a
factor, the location or package namewhere the method was found.
Note
The originalmethods function was written by Martin Maechler.
References
Chambers JM (1992).“Classes and Methods: Object-Oriented Programming in S.”In Chambers JM, Hastie TJ (eds.),Statistical Models in S.Wadsworth & Brooks/Cole.Appendix A.
See Also
For S4,getMethod,showMethods,Introduction orMethods_Details.
Examples
methods(class = "MethodsFunction") # format and printrequire(stats)methods(summary)methods(class = "aov") # S3 class## The same, with more details and more difficult to read:print(methods(class = "aov"), byclass=FALSE)methods("[[") # uses C-internal dispatchingmethods("$")methods("$<-") # replacement functionmethods("+") # binary operatormethods("Math") # group genericrequire(graphics)methods(axis) # looks like a generic, but is notmf <- methods(format) # quite a few; ... the last few :tail(cbind(meth = format(mf)))if(require(Matrix, quietly = TRUE)) {print(methods(class = "Matrix")) # S4 classm <- methods(dim) # S3 and S4 methodsprint(m)print(attr(m, "info")) # more extensive information## --> help(showMethods) for related examples}