This help topic is for R version 1.7.1. For the current version of R, try https://stat.ethz.ch/R-manual/R-patched/library/base/html/methods.html
methods {base}R Documentation

Class Methods

Description

R possesses a simple generic function mechanism which can be used for an object-oriented style of programming. Method despatch takes place based on the class of the first argument to the generic function or on the object supplied as an argument to UseMethod or NextMethod.

Usage

UseMethod(generic, object)
NextMethod(generic = NULL, object = NULL, ...)
methods(generic.function, class)

Arguments

generic

a character string naming a function.

object

an object whose class will determine the method to be dispatched. Defaults to the first argument of the enclosing function.

...

further arguments to be passed to the method.

generic.function

a generic function, or a character string naming a generic function.

class

a symbol or character string naming a class: only used if generic.function is not supplied.

Details

An R “object” is a data object which has a class attribute. A class attribute is a character vector giving the names of the classes which the object “inherits” from. If the object does not have a class attribute, it has an implicit class, "matrix", "array" or the result of mode(x).

When a generic function fun is applied to an object with class attribute c("first", "second"), the system searches for a function called fun.first and, if it finds it, applied it to the object. If no such function is found a function called fun.second is tried. If no class name produces a suitable function, the function fun.default is used.

Function methods can be used to find out about the methods for a particular generic function or class. The functions listed are those which are named like methods and may not actually be methods (known exceptions are discarded in the code). Note that the listed methods may not be user-visible objects, but often help will be available for them. (methods(class=) only reports user-visible methods.)

Now for some obscure details that need to appear somewhere. These comments will be slightly different than those in Appendix A of the White S Book. UseMethod creates a “new” function call with arguments matched as they came in to the generic. Any local variables defined before the call to UseMethod are retained (!?). Any statements after the call to UseMethod will not be evaluated as UseMethod does not return.

NextMethod invokes the next method (determined by the class). It does this by creating a special call frame for that method. The arguments will be the same in number, order and name as those to the current method but their values will be promises to evaluate their name in the current method and environment. Any arguments matched to ... are handled specially. They are passed on as the promise that was supplied as an argument to the current environment. (S does this differently!) If they have been evaluated in the current (or a previous environment) they remain evaluated.

NextMethod should not be called except in methods called by UseMethod. In particular it will not work inside anonymous calling functions (eg get("print.ts")(AirPassengers)).

Note

This scheme is called S3 (S version 3). For new projects, it is recommended to use the more flexible and robust S4 scheme provided in the methods package. Functions can have both S3 and S4 methods, and function showMethods will list the S4 methods (possibly none).
The function .isMethodsDispatchOn() returns TRUE if the new S4 methods are available but is meant for R internal use only.

The methods function was written by Martin Maechler.

References

Chambers, J. M. (1992) Classes and methods: object-oriented programming in S. Appendix A of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth \& Brooks/Cole.

See Also

class, getS3method

Examples

methods(summary)

## Not run: methods(print)

methods(class = data.frame)

methods("[")  ##- does not list the C-internal ones...

## End(Not run)

[Package base version 1.7.1 ]