UseMethod(generic, object, ...) NextMethod(generic, object, ...)
apply
will not
work otherwise.
It is also good style to include this argument if the name of the generic
function includes a period (
.
).
NULL
if there is no method (not
even a default method).
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
)
see
for the closest thing to an analog of
NextMethod
.
A generic function
abc
may call
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.
Chambers, J. M. and Hastie, T. J. (1991). Statistical Models in S. Wadsworth & Brooks/Cole, Pacific Grove, CA. (Appendix A)
# How function print uses methods print function(x, ...) .Call("S_c_use_method", "print", COPY = NULL, CLASSES = NULL) "print.foo" <- function(x, ...) { dd <- if(length(x) > 5) 4 else 12 x <- oldUnclass(x) #use invisible so the value won't be printed again invisible(NextMethod("print", digits = dd)) } x <- (1:6)/3.0 oldClass(x) <- "foo" print(x) # [1] 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000 # x <- (1:3)/3.0 oldClass(x) <- "foo" x # [1] 0.333333333333 0.666666666667 1.000000000000 # rm(print.foo) # Clean up after example