Define a Method for a Generic Function

DESCRIPTION:

Define a method for a generic function and save the necessary information in the metadata of an S chapter.

USAGE:

setMethod(f, signature = character(), 
          definition = selectMethod(f, signature), where = 1)
setDefaultMethod(f, 
          definition = selectMethod(f, signature), where = 1)

REQUIRED ARGUMENTS:

f
character; name of the generic function for which this method is being defined.

OPTIONAL ARGUMENTS:

signature
in the simplest case, the name of the class for which the method is defined. The new definition is used when the generic function is called and the first argument is of this class.

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
function definition for this method.
where
database where the generic function is stored. Supply an object defining the database or a number representing its position in the search path.

VALUE:

A function object; the value of definition.

SIDE EFFECTS:

The definition of function 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 ALSO:

, , , , .

See for further examples using setGeneric and related functions, and notes about the difference between S3 and S4 generic functions.

EXAMPLES:

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")