methods
package is a part of the R core distribution that implements S-language formal
classes and methods. Software written using this package can, with
some limitations, work
compatibly in R or in current versions of S-Plus.
S methods specialize S functions with different definitions depending on the class of one or more arguments in the call to the function. Methods allow you to adapt computations in S to particular classes, that is, types of data. The behavior of the function for other classes of arguments is not affected. The result is cleaner code, with changes and debugging localized to a small set of methods. At the same time, users of your software see the same functions as before; they don't need to learn new names or calling conventions to apply these functions to data from new classes.
Formal S class definitions specify what information a particular class of S objects should contain. They also allow classes to extend other classes; roughly, to contain all the information from one or more other, simpler classes. When this is true, the new class inherits methods defined for the older classes. Inheritance allows you to design new classes of data that are able to use existing methods. Explicit class definitions provide guarantees of what information objects contain.
Because the definitions of classes and methods are themselves stored as objects, computations can work with those definitions, a concept known as reflectance or meta data. Reflectance can be used to automate many useful programming tasks.
An introduction to the use of classes and methods is available in postscript or PDF format. This is adapted from a section of the book, Programming with Data. The material in the introduction has been adapted for compatibility between R and S-Plus. Much more extensive discussion of methods and classes is contained in the book itself.
Detailed documentation of the R functions is included in the package:
The expression
?setMethod
shows documentation related to methods and
the expression ?setClass
shows documentation related to classes.
Calls to the functions described in the help documents will be
compatible between R and S-Plus, except for a few extensions or
special cases noted in the
documentation.
Utility functions in the package are usually not compatible, since
the R implementation is totally different. Don't use undocumented
functions, or those documented as utility
functions if you want your own programming to
work for both systems (or, for that matter, if you don't want a future
revision of the package to break your code).
Detailed discussion of classes and methods is found in the Programming with Data book, particularly Chapters 7 and 8. The description there precedes the R implementation, so some of the material will not be found in R (at least not yet).
The methods package, and the system of methods and classes it implements are still evolving. The reference above documents one version, but there are a number of issues being discussed as we aim to improve and extend the mechanism. If you would like to look over some of the current issues, some very informal notes are available on the R developers' site.