UseMethod
Methods Invoked from Functions

Description

These are utility functions for object-oriented programming.

Usage

UseMethod(generic, object) 
NextMethod(generic = NULL, object = NULL, ...) 

Arguments

generic character string naming the generic function. While this can usually be inferred by the implementation in typical applications, it is strongly encouraged to supply this argument explicitly. For one thing, the semantics of the function containing the call is then independent of any object names. Specifically, use of the function in contexts like apply will not work otherwise. It is also good style to include this argument if the name of the generic function includes a period (.).
object the object to use in determining classes. By default, the first argument to the function is used; you need to specify the relevant argument if it is not the first one.
... other optional arguments pass to or from methods.

Details

These functions are used to implement SV3-style methods (methods defined using the naming convention function.class) such as plot.lm
For SV4-style methods (e.g. defined using setMethod) see callGeneric for the closest thing to an analog of NextMethod.
A generic function abc may call UseMethod in order to examine its first argument and dispatch a method appropriate to the argument's class. For example, if plot was given an argument whose class "lm", then function plot.lm will be dispatched if it exists. plot.lm will be also be called for an object that inherits from "lm" (e.g. if "lm" is the second class) if no earlier class has a method.
When UseMethod is executed, the frame of the function calling UseMethod is used to provide arguments to the dispatched method. The method matches its arguments from that frame. Since the method uses the original frame, commands after UseMethod in the generic function will not be executed.
The function NextMethod when called within the method, determines the next inherited method for this function and evaluates it, returning the value to the calling method. Calling NextMethod is the usual way to define a new method that elaborates on what would be done by an inherited method for the same function.
By default, the first argument to the generic function determines the relevant class, with the exception of infix operators. For operators, a method is defined if either of the operands defines a class for which a method exists; in the case that both operands define a method, the two classes must be the same.
Value
the value of the invoked method, or NULL if there is no method (not even a default method).
Side Effects
any side effects performed by the method that is used.
In addition, the DETAILS section discusses the manipulation of function frames that occurs.
References
Chambers, J. M. and Hastie, T .J. (Eds.) 1991. Statistical Models in S. Pacific Grove, CA.: Wadsworth & Brooks/Cole. Appendix A.
Chambers, J. M. 1992. Classes and methods: object-oriented programming in S. Statistical Models in S. Pacific Grove, CA.: Wadsworth & Brooks/Cole. Appendix A.
See Also
callGeneric, Methods, setMethod, class.
Examples
# How function print uses methods
print
# function (x, ...) 
# UseMethod("print")
"print.foo" <- function(x, ...)
{
   dd <- if(length(x) > 5) 4 else 12
    x <- unclass(x)
    # use invisible so the value won't be printed again
    invisible(NextMethod("print", digits = dd))
}
x <- (1:6)/3.0
class(x) <- "foo"
print(x)
# [1] 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000
#
x <- (1:3)/3.0
class(x) <- "foo"
x
# [1] 0.333333333333 0.666666666667 1.000000000000
#
rm(print.foo) # Clean up after example
Package base version 6.1.1-7
Package Index