The R Project SVN R

Rev

Rev 41161 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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