setMethod(f, signature = character(), definition = selectMethod(f, signature), where = 1) setDefaultMethod(f, definition = selectMethod(f, signature), where = 1)
More generally, a
named list matching the classes to the formal arguments of function
f
. The same rules are used as in matching positional and
named arguments in function calls. Any arguments, and any number of
arguments, can appear in the signature. Arguments that are missing
from the signature correspond to
"ANY"
; that is, they
match any class.
definition
.
f
is revised and an object assigned in the chapter. These side effects do not take place immediately; rather, they take effect on exit from the top-level frame.
See
for further examples using
setGeneric
and related functions,
and notes about the difference between S3 and S4 generic functions.
setMethod("sqrt", "matrix", function(x) chol(x)) setDefaultMethod("sqrt", function(x) UseMethod("sqrt")) # That makes the (S4) default method for sqrt an "S3 generic", # so that e.g. sqrt.foo is used when calling # sqrt(an object of class "foo") removeMethod("sqrt") # remove the default method removeMethod("sqrt", "matrix")