The R Project SVN R

Rev

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

Rev 25118 Rev 25127
1
\name{Methods}
1
\name{Methods}
2
\alias{Methods}
2
\alias{Methods}
3
\title{General Information on Methods}
3
\title{General Information on Methods}
4
\non_function{}
-
 
5
\description{
4
\description{
6
  This documentation section covers some general topics on how methods
5
  This documentation section covers some general topics on how methods
7
  work and how the \pkg{methods} package interacts with the rest of R.  The
6
  work and how the \pkg{methods} package interacts with the rest of R.  The
8
  information is usually not needed to get started with methods and
7
  information is usually not needed to get started with methods and
9
  classes, but may be helpful for moderately ambitious projects, or when
8
  classes, but may be helpful for moderately ambitious projects, or when
10
  something doesn't work as expected.
9
  something doesn't work as expected.
11
 
10
 
12
  The section \bold{How Methods Work} describes the underlying
11
  The section \bold{How Methods Work} describes the underlying
13
  mechanism; \bold{Class Inheritance and Method Selection} provides more
12
  mechanism; \bold{Class Inheritance and Method Selection} provides more
14
  details on how class definitions determine which methods are used.
13
  details on how class definitions determine which methods are used.
15
 
14
 
16
  The section \bold{Changes with the Methods Package} outlines possible
15
  The section \bold{Changes with the Methods Package} outlines possible
17
  effects on other computations when running with package \pkg{methods}.
16
  effects on other computations when running with package \pkg{methods}.
18
}
17
}
19
\section{How Methods Work}{
18
\section{How Methods Work}{
20
  A generic function is a function that has associated with it a
19
  A generic function is a function that has associated with it a
21
  collection of other functions (the methods), all of which agree in
20
  collection of other functions (the methods), all of which agree in
22
  formal arguments with the generic.  In R, the \dQuote{collection} is an
21
  formal arguments with the generic.  In R, the \dQuote{collection} is an
23
  object of class \code{"\link{MethodsList}"}, which contains a named
22
  object of class \code{"\link{MethodsList}"}, which contains a named
24
  list of methods (the \code{methods} slot), and the name of one of the
23
  list of methods (the \code{methods} slot), and the name of one of the
25
  formal arguments to the function (the \code{argument} slot).  The
24
  formal arguments to the function (the \code{argument} slot).  The
26
  names of the methods are the names of classes, and the corresponding
25
  names of the methods are the names of classes, and the corresponding
27
  element defines the method or methods to be used if the corresponding
26
  element defines the method or methods to be used if the corresponding
28
  argument has that class.  For example, suppose a function \code{f} has
27
  argument has that class.  For example, suppose a function \code{f} has
29
  formal arguments \code{x} and \code{y}.  The methods list object for
28
  formal arguments \code{x} and \code{y}.  The methods list object for
30
  that function has the object \code{as.name("x")} as its
29
  that function has the object \code{as.name("x")} as its
31
  \code{argument} slot.  An element of the methods named \code{"track"}
30
  \code{argument} slot.  An element of the methods named \code{"track"}
32
  is selected if the actual argument corresponding to \code{x} is an
31
  is selected if the actual argument corresponding to \code{x} is an
33
  object of class \code{"track"}.  If there is such an element, it can
32
  object of class \code{"track"}.  If there is such an element, it can
34
  generally be either a function or another methods list object.
33
  generally be either a function or another methods list object.
35
 
34
 
36
  In the first case, the function defines the method to use for any call
35
  In the first case, the function defines the method to use for any call
37
  in which \code{x} is of class \code{"track"}.  In the second case, the
36
  in which \code{x} is of class \code{"track"}.  In the second case, the
38
  new methods list object defines the selection of methods depending on
37
  new methods list object defines the selection of methods depending on
39
  the remaining formal arguments, in this example, \code{y}.  The same
38
  the remaining formal arguments, in this example, \code{y}.  The same
40
  selection process takes place, recursively, using the new methods list.
39
  selection process takes place, recursively, using the new methods list.
41
  Eventually, the selection returns either a function or \code{NULL},
40
  Eventually, the selection returns either a function or \code{NULL},
42
  meaning that no method matched the actual arguments.
41
  meaning that no method matched the actual arguments.
43
 
42
 
44
  Each method selected corresponds conceptually to a \emph{signature};
43
  Each method selected corresponds conceptually to a \emph{signature};
45
  that is a named list of classes, with names corresponding to some or
44
  that is a named list of classes, with names corresponding to some or
46
  all of the formal arguments.  In the previous example, if selecting
45
  all of the formal arguments.  In the previous example, if selecting
47
  class \code{"track"} for \code{x}, finding that the selection was
46
  class \code{"track"} for \code{x}, finding that the selection was
48
  another methods list and then selecting class \code{"numeric"} for
47
  another methods list and then selecting class \code{"numeric"} for
49
  \code{y} would produce a method associated with the signature
48
  \code{y} would produce a method associated with the signature
50
  \code{x = "track", y = "numeric"}.
49
  \code{x = "track", y = "numeric"}.
51
 
50
 
52
  The actual selection is done recursively, but you can see the methods
51
  The actual selection is done recursively, but you can see the methods
53
  arranged by signature by calling the function
52
  arranged by signature by calling the function
54
  \code{\link{showMethods}}, and objects with the methods arranged this
53
  \code{\link{showMethods}}, and objects with the methods arranged this
55
  way (in two different forms) are returned by the functions
54
  way (in two different forms) are returned by the functions
56
  \code{\link{listFromMlist}} and \code{\link{linearizeMlist}}.
55
  \code{\link{listFromMlist}} and \code{\link{linearizeMlist}}.
57
 
56
 
58
  In an R session, each generic function has a single methods list
57
  In an R session, each generic function has a single methods list
59
  object defining all the currently available methods.  The session
58
  object defining all the currently available methods.  The session
60
  methods list object is created the first time the function is called
59
  methods list object is created the first time the function is called
61
  by merging all the relevant method definitions currently visible.
60
  by merging all the relevant method definitions currently visible.
62
  Whenever something happens that might change the definitions (such as
61
  Whenever something happens that might change the definitions (such as
63
  attaching or detaching a package with methods for this function, or
62
  attaching or detaching a package with methods for this function, or
64
  explicitly defining or removing methods), the merged methods list
63
  explicitly defining or removing methods), the merged methods list
65
  object is removed.  The next call to the function will recompute the
64
  object is removed.  The next call to the function will recompute the
66
  merged definitions.
65
  merged definitions.
67
 
66
 
68
  When methods list are merged, they can come from two sources:
67
  When methods list are merged, they can come from two sources:
69
  \enumerate{
68
  \enumerate{
70
    \item Methods list objects for the same function anywhere on the
69
    \item Methods list objects for the same function anywhere on the
71
    current search list.  These are merged so that methods in an
70
    current search list.  These are merged so that methods in an
72
    environment earlier in the search list override methods for the same
71
    environment earlier in the search list override methods for the same
73
    function later in the search list.  A method overrides only 
72
    function later in the search list.  A method overrides only 
74
    another method for the same signature.  See the comments on class
73
    another method for the same signature.  See the comments on class
75
    \code{"ANY"} in the section on \bold{Inheritance}.
74
    \code{"ANY"} in the section on \bold{Inheritance}.
76
    \item Methods list objects corresponding the group generic
75
    \item Methods list objects corresponding the group generic
77
    functions, if any, for this function.  Any generic function can be
76
    functions, if any, for this function.  Any generic function can be
78
    defined to belong to a group generic.  The methods for the group
77
    defined to belong to a group generic.  The methods for the group
79
    generic are available as methods for this function.  The group
78
    generic are available as methods for this function.  The group
80
    generic can itself be defined as belong to a group; as a result
79
    generic can itself be defined as belong to a group; as a result
81
    there is a list of group generic functions.  A method defined for a
80
    there is a list of group generic functions.  A method defined for a
82
    function and a particular signature overrides a method for the same
81
    function and a particular signature overrides a method for the same
83
    signature for that function's group generic.
82
    signature for that function's group generic.
84
  }
83
  }
85
  Merging is done first on all methods for a particular function, and
84
  Merging is done first on all methods for a particular function, and
86
  then over the generic and its group generics.
85
  then over the generic and its group generics.
87
 
86
 
88
  The result is a single methods list object that contains all the
87
  The result is a single methods list object that contains all the
89
  methods \emph{directly} defined for this function.  As calls to the
88
  methods \emph{directly} defined for this function.  As calls to the
90
  function occur, this information may be supplemented by
89
  function occur, this information may be supplemented by
91
  \emph{inherited} methods, which we consider next.
90
  \emph{inherited} methods, which we consider next.
92
}
91
}
93
\section{Class Inheritance and Method Selection}{
92
\section{Class Inheritance and Method Selection}{
94
If no method is found directly for the actual arguments in a call to a
93
If no method is found directly for the actual arguments in a call to a
95
generic function, an attempt is made to match the available methods to
94
generic function, an attempt is made to match the available methods to
96
the arguments by using \emph{inheritance}.
95
the arguments by using \emph{inheritance}.
97
 
96
 
98
Each class definition potentially includes the names of one or more
97
Each class definition potentially includes the names of one or more
99
classes that the new class contains.  (These are sometimes called the
98
classes that the new class contains.  (These are sometimes called the
100
\emph{superclasses} of the new class.)  These classes themselves may
99
\emph{superclasses} of the new class.)  These classes themselves may
101
extend other classes.  Putting all this information together produces
100
extend other classes.  Putting all this information together produces
102
the full list of superclasses for this class. (You can see this list
101
the full list of superclasses for this class. (You can see this list
103
for any class \code{"A"} from the expression \code{extends("A")}.)
102
for any class \code{"A"} from the expression \code{extends("A")}.)
104
In addition, any class implicitly extends class \code{"ANY"}.
103
In addition, any class implicitly extends class \code{"ANY"}.
105
When all the superclasses are needed, as they are for dispatching
104
When all the superclasses are needed, as they are for dispatching
106
methods, they are ordered by how direct they are:  first, the direct
105
methods, they are ordered by how direct they are:  first, the direct
107
classes contained directly in the definition of this class, then the
106
classes contained directly in the definition of this class, then the
108
superclasses of these classes, etc.
107
superclasses of these classes, etc.
109
 
108
 
110
The S language has an additional, explicit mechanism for defining superclasses, the
109
The S language has an additional, explicit mechanism for defining superclasses, the
111
\code{\link{setIs}} mechanism.
110
\code{\link{setIs}} mechanism.
112
This mechanism allows a class to extend another even though they do
111
This mechanism allows a class to extend another even though they do
113
not have the same representation.  The extension is made possible by
112
not have the same representation.  The extension is made possible by
114
defining explicit methods to \code{coerce} an object to its superclass
113
defining explicit methods to \code{coerce} an object to its superclass
115
and to \code{replace} the data in the object corresponding to the
114
and to \code{replace} the data in the object corresponding to the
116
superclass.  The \code{\link{setIs}} mechanism will be used less often
115
superclass.  The \code{\link{setIs}} mechanism will be used less often
117
and only when directly including the superclass does not make sense,
116
and only when directly including the superclass does not make sense,
118
but once defined, the superclass acts just as directly contained
117
but once defined, the superclass acts just as directly contained
119
classes as far as method selection is concerned.
118
classes as far as method selection is concerned.
120
 
119
 
121
A method will be selected by inheritance if we can find a method in
120
A method will be selected by inheritance if we can find a method in
122
the methods list for a signature corresponding to any
121
the methods list for a signature corresponding to any
123
combination of superclasses for each of the relevant arguments.
122
combination of superclasses for each of the relevant arguments.
124
The search for such a method is performed by the function
123
The search for such a method is performed by the function
125
\code{\link{MethodsListSelect}}, working as follows.
124
\code{\link{MethodsListSelect}}, working as follows.
126
 
125
 
127
 
126
 
128
  The generic, \code{f} say,  has a signature, which by default
127
  The generic, \code{f} say,  has a signature, which by default
129
  is all its formal arguments, except \dots (see
128
  is all its formal arguments, except \dots (see
130
  \code{\link{setGeneric}}). For each of the formal arguments in that
129
  \code{\link{setGeneric}}). For each of the formal arguments in that
131
  signature, in order, the class of the actual argument is matched
130
  signature, in order, the class of the actual argument is matched
132
  against available methods.  A missing argument corresponds to class
131
  against available methods.  A missing argument corresponds to class
133
  \code{"missing"}.  If no method corresponds to the class of the
132
  \code{"missing"}.  If no method corresponds to the class of the
134
  argument, the evaluator looks for a method corresponding to the
133
  argument, the evaluator looks for a method corresponding to the
135
  the superclasses (the other classes that the actual class
134
  the superclasses (the other classes that the actual class
136
  extends, always including
135
  extends, always including
137
  \code{"ANY"}).  If no match is found, the dispatch fails, with an
136
  \code{"ANY"}).  If no match is found, the dispatch fails, with an
138
  error.  (But if there is a default method, that will always match.)
137
  error.  (But if there is a default method, that will always match.)
139
 
138
 
140
  If the match succeeds, it can find either a single method, or a
139
  If the match succeeds, it can find either a single method, or a
141
  methods list.  In the first case, the search is over, and returns
140
  methods list.  In the first case, the search is over, and returns
142
  the method.  In the second case, the search proceeds, with the
141
  the method.  In the second case, the search proceeds, with the
143
  next argument in the signature of the generic.  \emph{That} search
142
  next argument in the signature of the generic.  \emph{That} search
144
  may succeed or fail.  If it fails, the dispatch will try again with
143
  may succeed or fail.  If it fails, the dispatch will try again with
145
  the next best match for the current argument, if there is one.
144
  the next best match for the current argument, if there is one.
146
  The last match always corresponds to class \code{"ANY"}.
145
  The last match always corresponds to class \code{"ANY"}.
147
 
146
 
148
The effect of this definition of the selection process is to order all
147
The effect of this definition of the selection process is to order all
149
possible inherited methods, first by the superclasses for the first
148
possible inherited methods, first by the superclasses for the first
150
argument, then within this by the superclasses for the second
149
argument, then within this by the superclasses for the second
151
argument, and so on.
150
argument, and so on.
152
 
151
 
153
}
152
}
154
 
153
 
155
\section{Changes with the Methods Package}{
154
\section{Changes with the Methods Package}{
156
  The \pkg{methods} package is designed to leave other computations in R
155
  The \pkg{methods} package is designed to leave other computations in R
157
  unchanged.  There are, however, a few areas where the default
156
  unchanged.  There are, however, a few areas where the default
158
  functions and behavior are overridden when running with the methods
157
  functions and behavior are overridden when running with the methods
159
  package attached.  This section outlines those known to have some
158
  package attached.  This section outlines those known to have some
160
  possible effect.
159
  possible effect.
161
 
160
 
162
  \describe{
161
  \describe{
163
 
162
 
164
    \item{\code{class}:}{
163
    \item{\code{class}:}{
165
      The \pkg{methods} package enforces the notion that every object
164
      The \pkg{methods} package enforces the notion that every object
166
      has a class; in particular, \code{class(x)} is never \code{NULL},
165
      has a class; in particular, \code{class(x)} is never \code{NULL},
167
      as it would be for basic vectors, for example, when not using
166
      as it would be for basic vectors, for example, when not using
168
      \pkg{methods}. 
167
      \pkg{methods}. 
169
 
168
 
170
      In addition, when assigning a class, the value is required to be a
169
      In addition, when assigning a class, the value is required to be a
171
      single string.  (However, objects can have multiple class names if
170
      single string.  (However, objects can have multiple class names if
172
      these were generated by old-style class computations.  The methods
171
      these were generated by old-style class computations.  The methods
173
      package does not hide the \dQuote{extra} class names.)
172
      package does not hide the \dQuote{extra} class names.)
174
 
173
 
175
      Computations using the notion of \code{NULL} class attributes or
174
      Computations using the notion of \code{NULL} class attributes or
176
      of class attributes with multiple class names are not really
175
      of class attributes with multiple class names are not really
177
      compatible with the ideas in the \pkg{methods} package.  Formal
176
      compatible with the ideas in the \pkg{methods} package.  Formal
178
      classes and class inheritance are designed to give more flexible
177
      classes and class inheritance are designed to give more flexible
179
      and reliable implementations of similar ideas.
178
      and reliable implementations of similar ideas.
180
 
179
 
181
      If you do have to mix the two approaches, any operations that use
180
      If you do have to mix the two approaches, any operations that use
182
      class attributes in the old sense should be written in terms of
181
      class attributes in the old sense should be written in terms of
183
      \code{attr(x, "class")}, not \code{class(x)}.  In particular, test
182
      \code{attr(x, "class")}, not \code{class(x)}.  In particular, test
184
      for no class having been assigned with
183
      for no class having been assigned with
185
      \code{is.null(attr(x, "class"))}.
184
      \code{is.null(attr(x, "class"))}.
186
    }
185
    }
187
 
186
 
188
    \item{Printing:}{
187
    \item{Printing:}{
189
      To provide appropriate printing automatically for objects with
188
      To provide appropriate printing automatically for objects with
190
      formal class definitions, the \pkg{methods} package overrides
189
      formal class definitions, the \pkg{methods} package overrides
191
      \code{print.default}, to look for methods for the generic function
190
      \code{print.default}, to look for methods for the generic function
192
      \code{show}, and to use a default method for objects with formal
191
      \code{show}, and to use a default method for objects with formal
193
      class definitions.
192
      class definitions.
194
 
193
 
195
      The revised version of \code{print.default} is intended to produce
194
      The revised version of \code{print.default} is intended to produce
196
      identical printing to the original version for any object that
195
      identical printing to the original version for any object that
197
      does \emph{not} have a formally defined class, including honoring
196
      does \emph{not} have a formally defined class, including honoring
198
      old-style print methods.  So far, no exceptions are known.
197
      old-style print methods.  So far, no exceptions are known.
199
    }
198
    }
200
  }
199
  }
201
}
200
}
202
\references{
201
\references{
203
  The R package \pkg{methods} implements, with a few exceptions, the
202
  The R package \pkg{methods} implements, with a few exceptions, the
204
  programming interface for classes
203
  programming interface for classes
205
  and methods in the book \emph{Programming with Data} (John
204
  and methods in the book \emph{Programming with Data} (John
206
  M. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,
205
  M. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,
207
  and chapters 7 and 8.
206
  and chapters 7 and 8.
208
 
207
 
209
  While the programming interface for the \pkg{methods} package follows
208
  While the programming interface for the \pkg{methods} package follows
210
  the reference, the R software is an original implementation, so
209
  the reference, the R software is an original implementation, so
211
  details in the reference that reflect the S4 implementation may appear
210
  details in the reference that reflect the S4 implementation may appear
212
  differently in R.  Also, there are extensions to the programming
211
  differently in R.  Also, there are extensions to the programming
213
  interface developed more recently than the reference.  For a
212
  interface developed more recently than the reference.  For a
214
  discussion of details and ongoing development, see the web page 
213
  discussion of details and ongoing development, see the web page 
215
  \url{http://developer.r-project.org/methodsPackage.html} and the
214
  \url{http://developer.r-project.org/methodsPackage.html} and the
216
  pointers from that page.
215
  pointers from that page.
217
}
216
}
218
\seealso{
217
\seealso{
219
  \code{\link{setGeneric}},
218
  \code{\link{setGeneric}},
220
  \code{\link{setClass}}
219
  \code{\link{setClass}}
221
}
220
}
222
\keyword{programming}
221
\keyword{programming}
223
\keyword{classes}
222
\keyword{classes}
224
\keyword{methods}
223
\keyword{methods}