The R Project SVN R

Rev

Rev 17454 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17454 Rev 19029
Line 2... Line 2...
2
\alias{Methods}
2
\alias{Methods}
3
\alias{plot}
3
\alias{plot}
4
\title{General Information on Methods}
4
\title{General Information on Methods}
5
\non_function{}
5
\non_function{}
6
\description{
6
\description{
-
 
7
  This documentation section covers some general topics on how methods
-
 
8
  work and how the methods package interacts with the rest of R.  The
-
 
9
  information is usually not needed to get started with methods and
-
 
10
  classes, but may be helpful for moderately ambitious projects, or when
-
 
11
  something doesn't work as expected.
-
 
12
 
-
 
13
  The section \bold{How Methods Work} describes the underlying
-
 
14
  mechanism; \bold{Class Inheritance and Method Selection} provides more
-
 
15
  details on how class definitions determine which methods are used.
7
 
16
 
8
This documentation section covers some general topics on how methods work
-
 
9
and how the methods package interacts with the rest of R.
-
 
10
The information is usually not needed to get started with methods and
-
 
11
classes, but may be helpful for moderately ambitious projects, or when
-
 
12
something doesn't work as expected.
-
 
13
 
-
 
14
The section \bold{How Methods Work} 
-
 
15
describes the underlying mechanism; \bold{Class Inheritance and Method
-
 
16
  Selection} provides more details on how class definitions determine
-
 
17
which methods are used.
-
 
18
 
-
 
19
The section \bold{Changes with the Methods Package} outlines possible
17
  The section \bold{Changes with the Methods Package} outlines possible
20
effects on other computations when running with package
18
  effects on other computations when running with package \code{methods}.
21
\code{methods}.
-
 
22
 
-
 
23
  
-
 
24
}
19
}
25
\section{How Methods Work}{
20
\section{How Methods Work}{
-
 
21
  A generic function is a function that has associated with it a
-
 
22
  collection of other functions (the methods), all of which agree in
-
 
23
  formal arguments with the generic.  In R, the ``collection'' is an
-
 
24
  object of class \code{"\link{MethodsList}"}, which contains a named
-
 
25
  list of methods (the \code{methods} slot), and the name of one of the
-
 
26
  formal arguments to the function (the \code{argument} slot).  The
-
 
27
  names of the methods are the names of classes, and the corresponding
-
 
28
  element defines the method or methods to be used if the corresponding
-
 
29
  argument has that class.  For example, suppose a function \code{f} has
-
 
30
  formal arguments \code{x} and \code{y}.  The methods list object for
-
 
31
  that function has the object \code{as.name("x")} as its
-
 
32
  \code{argument} slot.  An element of the methods named \code{"track"}
-
 
33
  is selected if the actual argument corresponding to \code{x} is an
-
 
34
  object of class \code{"track"}.  If there is such an element, it can
-
 
35
  generally be either a function or another methods list object.
-
 
36
 
-
 
37
  In the first case, the function defines the method to use for any call
-
 
38
  in which \code{x} is of class \code{"track"}.  In the second case, the
-
 
39
  new methods list object defines the selection of methods depending on
-
 
40
  the remaining formal arguments, in this example, \code{y}.  The same
-
 
41
  selection process takes place, recursively, using the new methods list.
-
 
42
  Eventually, the selection returns either a function or \code{NULL},
-
 
43
  meaning that no method matched the actual arguments.
-
 
44
 
-
 
45
  Each method selected corresponds conceptually to a \emph{signature};
-
 
46
  that is a named list of classes, with names corresponding to some or
-
 
47
  all of the formal arguments.  In the previous example, if selecting
-
 
48
  class \code{"track"} for \code{x}, finding that the selection was
-
 
49
  another methods list and then selecting class \code{"numeric"} for
-
 
50
  \code{y} would produce a method associated with the signature
-
 
51
  \code{x = "track", y = "numeric"}.
-
 
52
 
-
 
53
  The actual selection is done recursively, but you can see the methods
-
 
54
  arranged by signature by calling the function
-
 
55
  \code{\link{showMethods}}, and objects with the methods arranged this
-
 
56
  way (in two different forms) are returned by the functions
-
 
57
  \code{\link{listFromMlist}} and \code{\link{linearizeMlist}}.
-
 
58
 
-
 
59
  In an R session, each generic function has a single methods list
-
 
60
  object defining all the currently available methods.  The session
-
 
61
  methods list object is created the first time the function is called
-
 
62
  by merging all the relevant method definitions currently visible.
-
 
63
  Whenever something happens that might change the definitions (such as
-
 
64
  attaching or detaching a package with methods for this function, or
-
 
65
  explicitly defining or removing methods), the merged methods list
-
 
66
  object is removed.  The next call to the function will recompute the
-
 
67
  merged definitions.
-
 
68
 
-
 
69
  When methods list are merged, they can come from two sources:
-
 
70
  \enumerate{
-
 
71
    \item {Methods list objects for the same function on one or more
-
 
72
      currently attached databases.  These are merged so that methods in
-
 
73
      a database earlier in the search list override methods for the
-
 
74
      same function later in the search list. A method overrides only
-
 
75
      another method for the same signature.  See the comments on class
-
 
76
      \code{"ANY"} in the section on \bold{Inheritance}.
-
 
77
    }
-
 
78
    \item {Methods list objects corresponding to the generic function
-
 
79
      itself and to the group generic functions, if any, for this
-
 
80
      function.  Any generic function can be defined to belong to a
-
 
81
      group generic.  The methods for the group generic are available as
-
 
82
      methods for this function.  The group generic can itself be
-
 
83
      defined as belong to a group; as a result there is a list of group
-
 
84
      generic functions.  A method defined for a function and a
-
 
85
      particular signature overrides a method for the same signature for
-
 
86
      that function's group generic.
-
 
87
    }
-
 
88
  }
-
 
89
  Merging is done first over databases for a particular function, and
-
 
90
  then over the generic and its group generics.
26
 
91
 
27
A generic function is a function that has
-
 
28
associated with it a
-
 
29
collection of other functions(the methods), all of which agree in
-
 
30
formal arguments with the generic.
-
 
31
In R, the ``collection'' is an object of class
-
 
32
\code{"\link{MethodsList}"}, which contains a named list of methods
-
 
33
(the \code{methods} slot),
-
 
34
and the name of one of the formal arguments to the function (the
-
 
35
\code{argument} slot).
-
 
36
The names of the methods are the names of classes, and the corresponding element
-
 
37
defines the method or methods to be used if the corresponding argument
-
 
38
has that class.
-
 
39
For example, suppose a function \code{f} has formal arguments
-
 
40
\code{x} and \code{y}.
-
 
41
The methods list object for that function has the object
-
 
42
\code{as.name("x")} as its \code{argument} slot.
-
 
43
An element of the methods named \code{"track"} is selected if the
-
 
44
actual argument corresponding to \code{x} is an object of class
-
 
45
\code{"track"}.
-
 
46
If there is such an element, it can generally be either a function or
-
 
47
another methods list object.
-
 
48
 
-
 
49
In the first case, the function defines the method to use for any call
-
 
50
in which \code{x} is of class
-
 
51
\code{"track"}.
-
 
52
In the second case, the new methods list object defines the selection
-
 
53
of methods depending on the remaining formal arguments,
-
 
54
in this example, \code{y}.
-
 
55
The same selection process takes place, recursively, using the new
-
 
56
methods list.
-
 
57
Eventually, the selection returns either a function or \code{NULL},
-
 
58
meaning that no method matched the actual arguments.
-
 
59
 
-
 
60
Each method selected corresponds conceptually to a \emph{signature};
-
 
61
that is a named list of classes, with names corresponding to some or
-
 
62
all of the formal arguments.
-
 
63
In the previous example, if selecting class \code{"track"} for
-
 
64
\code{x}, finding that the selection was another methods list and then
-
 
65
selecting class \code{"numeric"} for \code{y} would produce a method
-
 
66
associated with the signature \code{
-
 
67
   x = "track", y = "numeric"
-
 
68
}
-
 
69
 
-
 
70
The actual selection is done recursively, but you can see the methods
-
 
71
arranged by signature by calling the function
-
 
72
\code{\link{showMethods}}, and objects with the methods arranged
-
 
73
this way (in two different forms) are returned by the functions
-
 
74
\code{\link{listFromMlist}} and \code{\link{linearizeMlist}}.
-
 
75
 
-
 
76
In an R session, each generic function has a single methods list
-
 
77
object defining all the currently available methods.
-
 
78
The session methods list object is created the first time the function is called
-
 
79
by merging all the relevant method definitions currently visible.
-
 
80
Whenever something happens that
-
 
81
might change the definitions (such as attaching or detaching a package
-
 
82
with methods for this function, or explicitly defining or removing methods), the merged
-
 
83
methods list object is removed.  The next call to the function will
-
 
84
recompute the merged definitions.
-
 
85
 
-
 
86
When methods list are merged, they can come from two sources:
-
 
87
\enumerate{
-
 
88
\item {
-
 
89
Methods list objects for the same function on one or more currently
-
 
90
attached databases.
-
 
91
These are merged so that methods in a database earlier in the search
-
 
92
list override methods for the same function later in the search list.
-
 
93
A method overrides only another method for the same signature.  See
-
 
94
the comments on class \code{"ANY"} in the section on \bold{Inheritance}.
-
 
95
}
-
 
96
\item {Methods list objects corresponding to the
-
 
97
    generic function itself and to the group generic functions, if
-
 
98
    any, for this function.
-
 
99
    Any generic function can be defined to belong to a group generic.
-
 
100
    The methods for the group generic are available as methods for
-
 
101
    this function.  The group generic can itself be defined as belong
-
 
102
    to a group; as a result there is a list of group generic
-
 
103
    functions.  A method defined for a function and a particular
-
 
104
    signature overrides a method for the same signature for that
-
 
105
    function's group generic
-
 
106
}
-
 
107
}
-
 
108
Merging is done first over databases for a particular function, and
-
 
109
then over the generic and its group generics.
-
 
110
 
-
 
111
The result is a single methods list object that contains all the
92
  The result is a single methods list object that contains all the
112
methods \emph{directly} defined for this function.
93
  methods \emph{directly} defined for this function.  As calls to the
113
As calls to the function occur, this information may be supplemented
94
  function occur, this information may be supplemented by
114
by \emph{inherited} methods, which we consider next.
95
  \emph{inherited} methods, which we consider next.
115
}
96
}
116
\section{Class Inheritance and Method Selection}{
97
\section{Class Inheritance and Method Selection}{
117
If no method is found directly for the actual arguments in a call to a
98
If no method is found directly for the actual arguments in a call to a
118
generic function, an attempt is made to match the available methods to
99
generic function, an attempt is made to match the available methods to
119
the arguments by using \emph{inheritance}.
100
the arguments by using \emph{inheritance}.
Line 152... Line 133...
152
argument, and so on.
133
argument, and so on.
153
Superclasses are ordered by how direct they are:  first, the direct
134
Superclasses are ordered by how direct they are:  first, the direct
154
superclasses, then the superclasses of these classes.
135
superclasses, then the superclasses of these classes.
155
}
136
}
156
\section{Changes with the Methods Package}{
137
\section{Changes with the Methods Package}{
157
The methods package is designed to leave other computations in R
138
  The methods package is designed to leave other computations in R
158
unchanged.  There are, however, a few areas where the default
139
  unchanged.  There are, however, a few areas where the default
159
functions and behavior are overridden when running with the methods
140
  functions and behavior are overridden when running with the methods
160
package attached.
-
 
161
This section outlines those known to have some possible effect.
141
  package attached.  This section outlines those known to have some
-
 
142
  possible effect.
162
 
143
 
163
\describe{
144
  \describe{
164
 
145
 
165
\item{\code{class}:}{
146
    \item{\code{class}:}{
166
    The methods package enforces the notion that every object has a
147
      The methods package enforces the notion that every object has a
167
    class; in particular, \code{class(x)} is never \code{NULL}, as it
148
      class; in particular, \code{class(x)} is never \code{NULL}, as it
168
    would be for basic vectors, for example, when not using methods.
149
      would be for basic vectors, for example, when not using methods.
169
 
150
 
170
    In addition, when assigning a class, the value is required to be a
151
      In addition, when assigning a class, the value is required to be a
171
    single string.  (However, objects can have multiple class names if
152
      single string.  (However, objects can have multiple class names if
172
    these were generated by old-style class computations.  The methods
153
      these were generated by old-style class computations.  The methods
173
    package does not hide the ``extra'' class names.)
154
      package does not hide the ``extra'' class names.)
174
 
155
 
175
    Computations using the notion of \code{NULL} class attributes or
156
      Computations using the notion of \code{NULL} class attributes or
176
    of class attributes with multiple class names are not really
157
      of class attributes with multiple class names are not really
177
    compatible with the ideas in the methods package.  Formal classes
158
      compatible with the ideas in the methods package.  Formal classes
178
    and class inheritance are designed to give more flexible and
159
      and class inheritance are designed to give more flexible and
179
    reliable implementations of similar ideas.
160
      reliable implementations of similar ideas.
180
 
161
 
181
    If you do have to mix the two approaches, any operations that use
162
      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
163
      class attributes in the old sense should be written in terms of
183
    \code{attr(x, "class")}, not \code{class(x)}.  In particular, test
164
      \code{attr(x, "class")}, not \code{class(x)}.  In particular, test
184
    for no class having been assigned with \code{is.null(attr(x, "class"))}.
165
      for no class having been assigned with
185
}
-
 
186
 
-
 
187
\item{Printing}{
-
 
188
    To provide appropriate printing   automatically for
-
 
189
    objects with formal class definitions, the methods package
-
 
190
    overrides \code{print.default}, to look for methods for the
-
 
191
    generic function \code{show}, and to use a default method for
-
 
192
    objects with formal class definitions.
166
      \code{is.null(attr(x, "class"))}.
193
 
-
 
194
    The revised version of \code{print.default} is intended to produce
-
 
195
    identical printing to the original version for any object that
-
 
196
    does \emph{not} have a formally defined class, including honoring
-
 
197
    old-style print methods.  So far, no exceptions are known.
-
 
198
  }
-
 
199
 
-
 
200
  \item{\code{plot}}{
-
 
201
    A version of the \code{plot} function is included in the current
-
 
202
    methods package, differing from the one in the base package in that
-
 
203
    it has a \code{y} argument (necessary if methods for plot are to be
-
 
204
    defined for the \code{y} data; see the examples for
-
 
205
    \link{setMethod}).
-
 
206
    This version will move into base as soon as it is tested. 
-
 
207
    }
167
    }
208
 
168
 
-
 
169
    \item{Printing}{
-
 
170
      To provide appropriate printing automatically for objects with
-
 
171
      formal class definitions, the methods package overrides
-
 
172
      \code{print.default}, to look for methods for the generic function
-
 
173
      \code{show}, and to use a default method for objects with formal
-
 
174
      class definitions.
209
}
175
 
-
 
176
      The revised version of \code{print.default} is intended to produce
-
 
177
      identical printing to the original version for any object that
-
 
178
      does \emph{not} have a formally defined class, including honoring
-
 
179
      old-style print methods.  So far, no exceptions are known.
-
 
180
    }
210
 
181
 
-
 
182
    \item{\code{plot}}{
-
 
183
      A version of the \code{plot} function is included in the current
-
 
184
      methods package, differing from the one in the base package in
-
 
185
      that it has a \code{y} argument (necessary if methods for plot are
-
 
186
      to be defined for the \code{y} data; see the examples for
-
 
187
      \code{\link{setMethod}}).  This version will move into base as
-
 
188
      soon as it is tested.
-
 
189
    }
-
 
190
  }
211
}
191
}
212
 
-
 
213
\references{
192
\references{
214
The web page \url{http://www.omegahat.org/RSMethods/index.html} is the primary documentation.
193
  The web page \url{http://www.omegahat.org/RSMethods/index.html} is the
-
 
194
  primary documentation.
215
 
195
 
216
The functions in this package emulate the facility for classes and methods described in
196
  The functions in this package emulate the facility for classes and
217
\emph{Programming with Data}, (John M. Chambers, Springer, 1998).  See this book
197
  methods described in \emph{Programming with Data} (John M. Chambers,
218
for further details and examples.
198
  Springer, 1998).  See this book for further details and examples.
219
}
199
}
220
\author{
200
\author{
221
  John Chambers
201
  John Chambers
222
}
202
}
-
 
203
\seealso{
223
\seealso{\code{\link{setGeneric}}, \code{\link{setClass}}}
204
  \code{\link{setGeneric}},
-
 
205
  \code{\link{setClass}}
-
 
206
}
224
\keyword{programming}
207
\keyword{programming}
225
\keyword{classes}
208
\keyword{classes}
226
\keyword{methods}
209
\keyword{methods}