| 42333 |
ripley |
1 |
% File src/library/methods/man/Methods.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
|
|
3 |
% Copyright 1995-2007 R Core Development Team
|
|
|
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 15357 |
jmc |
6 |
\name{Methods}
|
| 16802 |
jmc |
7 |
\alias{Methods}
|
| 17000 |
jmc |
8 |
\title{General Information on Methods}
|
| 15357 |
jmc |
9 |
\description{
|
| 19029 |
hornik |
10 |
This documentation section covers some general topics on how methods
|
| 25118 |
hornik |
11 |
work and how the \pkg{methods} package interacts with the rest of R. The
|
| 19029 |
hornik |
12 |
information is usually not needed to get started with methods and
|
|
|
13 |
classes, but may be helpful for moderately ambitious projects, or when
|
|
|
14 |
something doesn't work as expected.
|
| 15357 |
jmc |
15 |
|
| 19029 |
hornik |
16 |
The section \bold{How Methods Work} describes the underlying
|
| 39016 |
jmc |
17 |
mechanism; \bold{Dispatch and Method Selection} provides more
|
| 19029 |
hornik |
18 |
details on how class definitions determine which methods are used.
|
| 39360 |
jmc |
19 |
For additional information specifically about class definitions, see \code{?\link{Classes}}.
|
| 39016 |
jmc |
20 |
}
|
| 16764 |
jmc |
21 |
|
|
|
22 |
\section{How Methods Work}{
|
| 19029 |
hornik |
23 |
A generic function is a function that has associated with it a
|
|
|
24 |
collection of other functions (the methods), all of which agree in
|
| 41161 |
maechler |
25 |
formal arguments with the generic.
|
| 39016 |
jmc |
26 |
|
| 39342 |
jmc |
27 |
Each R package will include methods metadata objects
|
| 39016 |
jmc |
28 |
corresponding to each generic function for which methods have been
|
|
|
29 |
defined in that package.
|
|
|
30 |
When the package is loaded into an R session, the methods for each
|
|
|
31 |
generic function are \emph{cached}, that is, stored in the
|
|
|
32 |
environment of the generic function along with the methods from
|
|
|
33 |
previously loaded packages. This merged table of methods is used to
|
|
|
34 |
dispatch or select methods from the generic, using class inheritance
|
| 41161 |
maechler |
35 |
and possibly group generic functions (see
|
|
|
36 |
\code{\link{S4groupGeneric}}) to find an applicable method.
|
| 39016 |
jmc |
37 |
See the \bold{Dispatch} section below.
|
| 39342 |
jmc |
38 |
The caching computations ensure that only one version of each
|
|
|
39 |
generic function is visible globally; although different attached
|
|
|
40 |
packages may contain a copy of the generic function, these are in
|
|
|
41 |
fact identical.
|
| 16764 |
jmc |
42 |
|
| 39016 |
jmc |
43 |
The methods for a generic are stored according to the
|
|
|
44 |
corresponding \code{signature} for which the method was defined, in
|
|
|
45 |
a call to \code{\link{setMethod}}. The signature associates one
|
|
|
46 |
class name with each of a subset of the formal arguments to the
|
|
|
47 |
generic function. Which formal arguments are available, and the
|
|
|
48 |
order in which they appear, are determined by the \code{"signature"}
|
|
|
49 |
slot of the generic function. By default, the signature of the
|
| 39342 |
jmc |
50 |
generic consists of all the formal arguments except \dots, in the
|
| 39016 |
jmc |
51 |
order they appear in the function definition.
|
| 16764 |
jmc |
52 |
|
| 39016 |
jmc |
53 |
Trailing arguments in the signature will be \emph{inactive} if no
|
|
|
54 |
method has yet been specified that included those arguments.
|
|
|
55 |
Inactive arguments are not needed or used in labeling the cached
|
|
|
56 |
methods. (The distinction does not change which methods are
|
|
|
57 |
dispatched, but ignoring inactive arguments does improve the
|
|
|
58 |
efficiency of dispatch. Thus, defining the generic signature to
|
|
|
59 |
contain the most useful arguments first can help efficiency
|
|
|
60 |
somewhat.)
|
| 16764 |
jmc |
61 |
|
| 39342 |
jmc |
62 |
All arguments in the signature of the generic function will be evaluated when the
|
|
|
63 |
function is called, rather than using the traditional lazy
|
| 39016 |
jmc |
64 |
evaluation rules of S. Therefore, it's important to \emph{exclude}
|
|
|
65 |
from the signature any arguments that need to be dealt with
|
|
|
66 |
symbolically (such as the first argument to function
|
| 39342 |
jmc |
67 |
\code{\link{substitute}}). Note that only actual arguments are
|
| 39016 |
jmc |
68 |
evaluated, not default expressions.
|
|
|
69 |
A missing argument enters into the method selection as class
|
|
|
70 |
\code{"missing"} and non-missing arguments according to their actual
|
|
|
71 |
class.
|
| 16764 |
jmc |
72 |
|
| 39016 |
jmc |
73 |
As of version 2.4.0 of R, the cached methods are stored in an
|
|
|
74 |
environment object. The names used for assignment are a
|
| 39342 |
jmc |
75 |
concatenation of the class names for the arguments in the active
|
| 41161 |
maechler |
76 |
signature.
|
| 16764 |
jmc |
77 |
|
| 39016 |
jmc |
78 |
}
|
|
|
79 |
\section{Dispatch and Method Selection}{
|
| 16764 |
jmc |
80 |
|
| 39016 |
jmc |
81 |
When a call to a generic function is evaluated, a method is selected corresponding
|
|
|
82 |
to the classes of the actual arguments in the signature.
|
|
|
83 |
First, the cached methods table is searched for a \emph{direct} match;
|
|
|
84 |
that is, a method stored under the direct class names.
|
|
|
85 |
The direct class is the value of \code{class(x)} for each non-missing
|
|
|
86 |
argument, and class \code{"missing"} for each missing argument.
|
| 16764 |
jmc |
87 |
If no method is found directly for the actual arguments in a call to a
|
|
|
88 |
generic function, an attempt is made to match the available methods to
|
|
|
89 |
the arguments by using \emph{inheritance}.
|
|
|
90 |
|
|
|
91 |
Each class definition potentially includes the names of one or more
|
| 23678 |
jmc |
92 |
classes that the new class contains. (These are sometimes called the
|
| 41161 |
maechler |
93 |
\emph{superclasses} of the new class.)
|
| 23678 |
jmc |
94 |
The S language has an additional, explicit mechanism for defining superclasses, the
|
|
|
95 |
\code{\link{setIs}} mechanism.
|
| 39016 |
jmc |
96 |
Also, a call to \code{\link{setClassUnion}} makes the union class a
|
|
|
97 |
superclass of each of the members of the union.
|
|
|
98 |
All three mechanisms are treated equivalently for purposes of
|
|
|
99 |
inheritance: they define the \emph{direct} superclasses of a
|
|
|
100 |
particular class.
|
| 23678 |
jmc |
101 |
|
| 39016 |
jmc |
102 |
The direct superclasses themselves may
|
|
|
103 |
contain other classes. Putting all this information together produces
|
| 41161 |
maechler |
104 |
the full list of superclasses for this class.
|
| 39016 |
jmc |
105 |
The superclass list is included in the definition of the class that is
|
|
|
106 |
cached during the R session.
|
|
|
107 |
Each element of the list describes the nature of the relationship (see
|
|
|
108 |
\link{SClassExtension-class} for details).
|
|
|
109 |
Included in the element is a \code{distance} slot giving a numeric
|
|
|
110 |
distance between the two classes.
|
|
|
111 |
The distance currently is the path length for the relationship:
|
|
|
112 |
\code{1} for direct superclasses (regardless of which mechanism
|
|
|
113 |
defined them), then \code{2} for the direct superclasses of those
|
|
|
114 |
classes, and so on.
|
|
|
115 |
In addition, any class implicitly has class \code{"ANY"} as a superclass. The
|
|
|
116 |
distance to \code{"ANY"} is treated as larger than the distance to any
|
|
|
117 |
actual class.
|
|
|
118 |
The special class \code{"missing"} corresponding to missing arguments
|
|
|
119 |
has only \code{"ANY"} as a superclass, while \code{"ANY"} has no
|
|
|
120 |
superclasses.
|
| 16764 |
jmc |
121 |
|
| 39016 |
jmc |
122 |
The information about superclasses is summarized when a class
|
|
|
123 |
definition is printed.
|
| 16764 |
jmc |
124 |
|
| 39016 |
jmc |
125 |
When a method is to be selected by inheritance, a search is made in
|
|
|
126 |
the table for all methods directly corresponding to a combination of
|
|
|
127 |
either the direct class or one of its superclasses, for each argument
|
|
|
128 |
in the active signature.
|
| 39342 |
jmc |
129 |
For an example, suppose there is only one argument in the signature and that the class of
|
| 39016 |
jmc |
130 |
the corresponding object was \code{"dgeMatrix"} (from the
|
|
|
131 |
\code{Matrix} package on CRAN).
|
| 39257 |
jmc |
132 |
This class has two direct superclasses and through these 4 additional superclasses.
|
| 39016 |
jmc |
133 |
Method selection finds all the methods in the table of directly
|
| 39257 |
jmc |
134 |
specified methods labeled by one of these classes, or by
|
| 39016 |
jmc |
135 |
\code{"ANY"}.
|
| 16764 |
jmc |
136 |
|
| 39016 |
jmc |
137 |
When there are multiple arguments in the signature, each argument will
|
|
|
138 |
generate a similar list of inherited classes.
|
| 39342 |
jmc |
139 |
The possible matches are now all the combinations of classes from each
|
| 39016 |
jmc |
140 |
argument (think of the function \code{outer} generating an array of
|
| 39342 |
jmc |
141 |
all possible combinations).
|
|
|
142 |
The search now finds all the methods matching any of this combination
|
| 39016 |
jmc |
143 |
of classes.
|
|
|
144 |
The computation of distances also has to combine distances for the
|
|
|
145 |
individual arguments.
|
|
|
146 |
There are many ways to combine the distances; the current
|
|
|
147 |
implementation simply adds them.
|
|
|
148 |
The result of the search is then a list of zero, one or more methods,
|
|
|
149 |
and a parallel vector of distances between the target signature and
|
|
|
150 |
the available methods.
|
| 23678 |
jmc |
151 |
|
| 39016 |
jmc |
152 |
If the list has more than one matching method, only those corresponding to
|
|
|
153 |
the minimum distance are considered.
|
|
|
154 |
There may still be multiple best methods.
|
|
|
155 |
The dispatch software considers this an ambiguous case and warns the
|
|
|
156 |
user (only on the first call for this selection).
|
|
|
157 |
The method occurring first in the list of superclasses is selected. By the mechanism of producing
|
|
|
158 |
the extension information, this orders the direct superclasses by the
|
|
|
159 |
order they appeared in the original call to \code{\link{setClass}},
|
|
|
160 |
followed by classes specified in \code{\link{setIs}} calls, in the
|
|
|
161 |
order those calls were evaluated, followed by classes specified in
|
|
|
162 |
unions.
|
|
|
163 |
Then the superclasses of those classes are appended (note that only
|
|
|
164 |
the ordering of classes within a particular generation of superclasses
|
|
|
165 |
counts, because only these will have the same distance).
|
| 39257 |
jmc |
166 |
For further discussion of method selection, see the document \url{http://developer.r-project.org/howMethodsWork.pdf}.
|
| 23678 |
jmc |
167 |
|
| 39016 |
jmc |
168 |
All this detail about selection is less important than the realization
|
|
|
169 |
that having ambiguous method selection usually means that you need to
|
|
|
170 |
be more specific about intentions.
|
|
|
171 |
It is likely that some consideration other than the ordering of
|
|
|
172 |
superclasses in the class definition is more important in determining
|
|
|
173 |
which method \emph{should} be selected, and the preference may well
|
|
|
174 |
be different for different generic functions. Where ambiguities
|
|
|
175 |
arise, the best approach is usually to provide a specific method for
|
|
|
176 |
the subclass.
|
| 23678 |
jmc |
177 |
|
| 39016 |
jmc |
178 |
When the inherited method has been selected, the selection is cached
|
|
|
179 |
in the generic function so that future calls with the same class will
|
|
|
180 |
not require repeating the search. Cached non-direct selections are
|
|
|
181 |
not themselves used in inheritance searches, since that could result
|
|
|
182 |
in invalid selections.
|
| 16559 |
jmc |
183 |
|
| 39016 |
jmc |
184 |
Besides being initiated through calls to the generic function, method
|
|
|
185 |
selection can be done explicitly by calling the function \code{\link{selectMethod}}.
|
| 16559 |
jmc |
186 |
|
| 39016 |
jmc |
187 |
}
|
| 16559 |
jmc |
188 |
|
| 15357 |
jmc |
189 |
\references{
|
| 25118 |
hornik |
190 |
The R package \pkg{methods} implements, with a few exceptions, the
|
| 23678 |
jmc |
191 |
programming interface for classes
|
|
|
192 |
and methods in the book \emph{Programming with Data} (John
|
|
|
193 |
M. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,
|
|
|
194 |
and chapters 7 and 8.
|
| 15357 |
jmc |
195 |
|
| 25118 |
hornik |
196 |
While the programming interface for the \pkg{methods} package follows
|
|
|
197 |
the reference, the R software is an original implementation, so
|
|
|
198 |
details in the reference that reflect the S4 implementation may appear
|
| 23678 |
jmc |
199 |
differently in R. Also, there are extensions to the programming
|
| 39257 |
jmc |
200 |
interface developed more recently than the reference.
|
| 39303 |
hornik |
201 |
}
|
| 19029 |
hornik |
202 |
\seealso{
|
|
|
203 |
\code{\link{setGeneric}},
|
|
|
204 |
\code{\link{setClass}}
|
| 39257 |
jmc |
205 |
and the document \url{http://developer.r-project.org/howMethodsWork.pdf}.
|
| 19029 |
hornik |
206 |
}
|
| 15357 |
jmc |
207 |
\keyword{programming}
|
|
|
208 |
\keyword{classes}
|
|
|
209 |
\keyword{methods}
|