The R Project SVN R

Rev

Rev 71460 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 71460 Rev 71468
Line 6... Line 6...
6
\name{as}
6
\name{as}
7
\alias{as}
7
\alias{as}
8
\alias{as<-}
8
\alias{as<-}
9
\title{Force an Object to Belong to a Class}
9
\title{Force an Object to Belong to a Class}
10
\description{
10
\description{
11
  These functions manage the relations that allow coercing an object to
-
 
12
  a given class.
11
  Coerce an object to a given class.
13
}
12
}
14
\usage{
13
\usage{
15
as(object, Class, strict=TRUE, ext)
14
as(object, Class, strict=TRUE, ext)
16
 
15
 
17
as(object, Class) <- value
16
as(object, Class) <- value
18
 
17
 
19
}
18
}
20
\section{Summary of Functions}{
19
\section{Description}{
21
  \describe{
-
 
22
    \item{\code{as}:}{
20
    \code{as(object)}
23
      Returns the version of this object coerced to be the given
21
      returns the version of this object coerced to be the given
24
      \code{Class}.  When used in the replacement form on the left of
22
      \code{Class}.  When used in the replacement form on the left of
25
      an assignment, the portion of the object corresponding to
23
      an assignment, the portion of the object corresponding to
26
      \code{Class} is replaced by \code{value}.
24
      \code{Class} is replaced by \code{value}.
27
 
25
 
28
      The operation of \code{as()} in either form depends on the
26
      The operation of \code{as()} in either form depends on the
29
      definition of coerce methods.  Methods are defined automatically
27
      definition of coerce methods.  Methods are defined automatically
30
      when the two classes are related by inheritance; that is, when
28
      when the two classes are related by inheritance; that is, when
31
      one of the classes is a subclass of the other.  See the section
29
      one of the classes is a subclass of the other.
32
      on inheritance  below for details.
-
 
33
 
30
 
34
      Coerce methods are also predefined for basic classes (including all
31
      Coerce methods are also predefined for basic classes (including all
35
      the types of vectors, functions and a few others). See
32
      the types of vectors, functions and a few others).
36
      \code{showMethods(coerce)} for a list of these.
-
 
37
 
33
 
38
      Beyond these two sources of methods, further methods are defined
34
      Beyond these two sources of methods, further methods are defined
39
      by calls to the \code{setAs} function.
35
      by calls to the \code{\link{setAs}} function.  See that
40
 
-
 
41
    }
-
 
42
 
-
 
43
    \item{\code{setAs}:}{
-
 
44
      Define methods for coercing an object of class \code{from} to be of class \code{to}; the \code{def} argument provides for direct coercing and the \code{replace} argument, if included, provides for replacement.  See the \dQuote{How} section below for details.
36
      documentation also for details of how coerce methods work. Use
45
    }
-
 
46
 
-
 
47
    \item{\code{coerce}, \code{coerce<-}:}{
-
 
48
      Coerce \code{from} to be of the same class as \code{to}.
-
 
49
 
-
 
50
      These functions should not be called explicitly.  The function
-
 
51
      \code{\link{setAs}} creates methods for them for the
37
      \code{showMethods(coerce)} for a list of all currently defined methods, as in the
52
      \code{as} function to use.
38
      example below.
53
    }
-
 
54
 
39
 
-
 
40
 
55
  }
41
  }
56
}
-
 
57
\arguments{
42
\arguments{
58
  \item{object}{any \R object.}
43
  \item{object}{any \R object.}
59
  \item{Class}{the name of the class to which \code{object} should be
44
  \item{Class}{the name of the class to which \code{object} should be
60
    coerced. }
45
    coerced. }
61
  \item{strict}{logical flag.  If \code{TRUE}, the returned object
46
  \item{strict}{logical flag.  If \code{TRUE}, the returned object
Line 69... Line 54...
69
    roughly, one that just adds slots to an existing class.}
54
    roughly, one that just adds slots to an existing class.}
70
  \item{value}{The value to use to modify \code{object} (see the
55
  \item{value}{The value to use to modify \code{object} (see the
71
    discussion below).  You should supply an object with class
56
    discussion below).  You should supply an object with class
72
    \code{Class}; some coercion is done, but you're unwise to rely on
57
    \code{Class}; some coercion is done, but you're unwise to rely on
73
    it.}
58
    it.}
74
 
-
 
75
  \item{from, to}{The classes between which the coerce methods
-
 
76
      \code{def} and \code{replace} perform coercion.
-
 
77
    }
-
 
78
  \item{def}{function of one argument.  It will get an object from
-
 
79
    class \code{from} and had better return an object of class
-
 
80
    \code{to}. The convention is that
-
 
81
    the name of the argument is \code{from}; if another argument name
-
 
82
    is used, \code{setAs} will attempt to substitute \code{from}. }
-
 
83
  \item{replace}{if supplied, the function to use as a replacement
-
 
84
    method, when \code{as} is used on the left of an assignment.
-
 
85
    Should be a function of two arguments, \code{from, value},
-
 
86
    although \code{setAs} will attempt to substitute if the arguments differ.}
-
 
87
  \item{where}{the position or environment in which to store the
-
 
88
    resulting methods. For most applications, it is recommended to
-
 
89
    omit this argument and to include the call to \code{setAs} in source code that is evaluated at the top level; that is, either in an R session by something equivalent to a call to \code{\link{source}}, or as part of the R source code for a package.}
-
 
90
  \item{ext}{the optional object
59
  \item{ext}{an optional object
91
    defining how \code{Class} is extended by the class of the
60
    defining how \code{Class} is extended by the class of the
92
    object (as returned by \code{\link{possibleExtends}}).
61
    object (as returned by \code{\link{possibleExtends}}).
93
    This argument is used internally (to provide essential
62
    This argument is used internally;
94
    information for non-public classes), but you are unlikely to want
-
 
95
    to use it directly.
63
    do not use it directly.
96
  }
64
  }
97
}
65
}
98
 
66
 
99
\section{Inheritance and Coercion}{
-
 
100
 
-
 
101
 Objects from one class can turn into objects from another class
-
 
102
  either automatically or by an explicit call to the \code{as}
-
 
103
  function.  Automatic conversion is special, and comes from the
-
 
104
  designer of one class of objects asserting that this class extends
-
 
105
  another class.  The most common case is that one or more class names
-
 
106
  are supplied in the \code{contains=} argument to \code{setClass}, in
-
 
107
  which case the new class extends each of the earlier classes (in the
-
 
108
  usual terminology, the earlier classes are \emph{superclasses} of
-
 
109
  the new class and it is a \emph{subclass} of each of them).
-
 
110
 
-
 
111
This form of inheritance is called \emph{simple} inheritance in \R.
-
 
112
See \code{\link{setClass}} for details.
-
 
113
Inheritance can also be defined explicitly by a call to
-
 
114
\code{\link{setIs}}.
-
 
115
The two versions have slightly different implications for coerce methods.
-
 
116
Simple inheritance implies that inherited slots behave identically in the subclass and the superclass.
-
 
117
Whenever two classes are related by simple inheritance, corresponding coerce methods
-
 
118
are defined for both direct and replacement use of \code{as}.
-
 
119
In the case of simple inheritance, these methods do the obvious
-
 
120
computation:  they extract or replace the slots in the object that
-
 
121
correspond to those in the superclass definition.
-
 
122
 
-
 
123
The implicitly defined coerce methods may be overridden by a call
-
 
124
to \code{setAs}; note, however, that the implicit methods are defined for each
-
 
125
subclass-superclass pair, so that you must override each of these
-
 
126
explicitly, not rely on inheritance.
-
 
127
 
-
 
128
When inheritance is defined by a call to \code{setIs}, the coerce methods are provided explicitly, not generated automatically.
-
 
129
Inheritance will apply (to the \code{from} argument, as described in  the section below).
-
 
130
You could also supply methods via \code{setAs} for non-inherited relationships, and now these also can be inherited.
-
 
131
 
67
 
132
For further on the distinction between simple and explicit inheritance, see \code{\link{setIs}}.
-
 
133
 
-
 
134
}
-
 
135
\section{How Functions 'as' and 'setAs' Work}{
-
 
136
  The function \code{as}  turns \code{object} into an object
-
 
137
  of class \code{Class}.  In doing so, it applies a \dQuote{coerce
-
 
138
    method}, using S4
-
 
139
  classes and methods, but in a somewhat special way.
-
 
140
Coerce methods are methods for the function \code{coerce} or, in the
-
 
141
replacement case the function \code{`coerce<-`}.
-
 
142
These functions have two arguments in method signatures, \code{from}
-
 
143
and \code{to}, corresponding to the class of the object and the
-
 
144
desired coerce class.
-
 
145
These functions must not be called directly, but are used to store
-
 
146
tables of methods for the use of \code{as}, directly and for
-
 
147
replacements.
-
 
148
In this section we will describe the direct case, but except where
-
 
149
noted the replacement case works the same way, using \code{`coerce<-`}
-
 
150
and the \code{replace} argument to \code{setAs}, rather than
-
 
151
\code{coerce} and the \code{def} argument.
-
 
152
 
-
 
153
Assuming the \code{object} is not already of the desired class,
-
 
154
\code{as} first looks for a method in the table of methods
-
 
155
 for the function
-
 
156
  \code{coerce} for the signature \code{c(from = class(object), to =
-
 
157
    Class)}, in the same way method selection would do its initial lookup.
-
 
158
To be precise, this means the table of both direct and inherited
-
 
159
methods, but inheritance is used specially in this case (see below).
-
 
160
 
-
 
161
If no method is found, \code{as} looks for one.
-
 
162
First, if either \code{Class} or \code{class(object)} is a superclass
-
 
163
of the other, the class definition will contain the information needed
-
 
164
to construct a coerce method.
-
 
165
In the usual case that the subclass contains the superclass (i.e., has
-
 
166
all its slots), the method is constructed either by extracting or
-
 
167
replacing the inherited slots.
-
 
168
Non-simple extensions (the result of a call to \code{\link{setIs}})
-
 
169
will usually contain explicit methods, though possibly not for replacement.
-
 
170
 
-
 
171
 If no subclass/superclass relationship provides a method, \code{as}
-
 
172
 looks for an inherited method, but applying, inheritance for the argument \code{from} only, not for
-
 
173
  the argument \code{to} (if you think about it, you'll probably agree
-
 
174
  that you wouldn't want the result to be from some class other than the
-
 
175
  \code{Class} specified). Thus,
-
 
176
  \code{selectMethod("coerce", sig, useInherited= c(from=TRUE, to= FALSE))}
-
 
177
  replicates the method selection used by \code{as()}.
-
 
178
 
-
 
179
In nearly all cases the method found in this way will be cached in the
-
 
180
table of coerce methods (the exception being subclass relationships with a test, which
-
 
181
are legal but discouraged).
-
 
182
So the detailed calculations should be done only on the first
-
 
183
occurrence of a coerce from \code{class(object)} to \code{Class}.
-
 
184
 
-
 
185
Note that  \code{coerce} is not a standard generic function.  It is
-
 
186
not intended to be called directly.  To prevent accidentally caching
-
 
187
an invalid inherited method, calls are routed to an equivalent call to
-
 
188
\code{as}, and a warning is issued.  Also, calls to
-
 
189
\code{\link{selectMethod}} for this function may not represent the
-
 
190
method that \code{as} will choose.  You can only trust the result if
-
 
191
the corresponding call to \code{as} has occurred previously in this
-
 
192
session.
-
 
193
 
-
 
194
  With this explanation as background, the function \code{setAs} does a
-
 
195
  fairly obvious computation:  It constructs and sets a method for the function
-
 
196
  \code{coerce} with signature \code{c(from, to)}, using the \code{def}
-
 
197
  argument to define the body of the method.  The function supplied as
-
 
198
  \code{def} can have one argument (interpreted as an object to be
-
 
199
  coerced) or two arguments (the \code{from} object and the \code{to}
-
 
200
  class).  Either way, \code{setAs} constructs a function of two
-
 
201
  arguments, with the second defaulting to the name of the \code{to}
-
 
202
  class.  The method will be called from \code{as} with the object
-
 
203
  as the \code{from} argument and no \code{to} argument, with the default for this argument being the name of the intended
-
 
204
  \code{to} class, so the method can use this information in messages.
-
 
205
 
-
 
206
The direct version of the \code{as} function also has a \code{strict=} argument that defaults to \code{TRUE}.
-
 
207
Calls during the evaluation of methods for other functions will set this argument to \code{FALSE}.
-
 
208
The distinction is relevant when the object being coerced is from a simple subclass of the \code{to} class; if \code{strict=FALSE} in this case, nothing need be done.
-
 
209
For most user-written coerce methods, when the two classes have no subclass/superclass, the \code{strict=} argument is irrelevant.
-
 
210
 
-
 
211
The \code{replace} argument to \code{setAs} provides a method for
-
 
212
\code{`coerce<-`}.
-
 
213
As with all replacement methods, the last argument of the method must
-
 
214
have the name \code{value} for the object on the right of the
-
 
215
assignment.
-
 
216
As with the \code{coerce} method, the first two arguments are
-
 
217
\code{from, to}; there is no \code{strict=} option for the replace case.
-
 
218
 
-
 
219
  The function \code{coerce} exists as a repository for
-
 
220
  such methods, to be selected as described above by the \code{as}
-
 
221
  function.  Actually dispatching the methods using
-
 
222
  \code{standardGeneric} could produce incorrect inherited methods, by using
-
 
223
   inheritance on the
-
 
224
  \code{to} argument; as mentioned, this is not the logic used for
-
 
225
  \code{as}.
-
 
226
  To prevent selecting and caching invalid methods, calls to
-
 
227
  \code{coerce} are
-
 
228
  currently mapped into calls to \code{as}, with a warning message.
-
 
229
}
-
 
230
 
68
 
231
 
69
 
232
\section{Basic Coercion Methods}{
70
\section{Basic Coercion Methods}{
233
  Methods are pre-defined for coercing any object to one of the basic
71
  Methods are pre-defined for coercing any object to one of the basic
234
  datatypes.  For example, \code{as(x, "numeric")} uses the existing
72
  datatypes.  For example, \code{as(x, "numeric")} uses the existing
235
  \code{as.numeric} function.  These built-in methods can be listed by
73
  \code{as.numeric} function.  These and all other existing methods
236
  \code{showMethods("coerce")}.
74
  can be listed as shown in the example.
237
}
75
}
238
 
76
 
239
\seealso{
77
\seealso{
240
  If you think of using \code{try(as(x, cl))}, consider
78
  If you think of using \code{try(as(x, cl))}, consider
241
  \code{\link{canCoerce}(x, cl)} instead.
79
  \code{\link{canCoerce}(x, cl)} instead.
242
}
80
}
243
\references{
-
 
244
 Chambers, John M. (2008)
-
 
245
 \emph{Software for Data Analysis: Programming with R}
-
 
246
  Springer.  (For the R version.)
-
 
247
 
-
 
248
 Chambers, John M. (1998)
-
 
249
 \emph{Programming with Data}
-
 
250
 Springer (For the original S4 version.)
-
 
251
}
-
 
252
\examples{
-
 
253
## using the definition of class "track" from \link{setClass}
-
 
254
 
-
 
255
\dontshow{
-
 
256
setClass("track", slots = c(x="numeric", y="numeric"))
-
 
257
setClass("trackCurve", contains = "track", slots = c(smooth = "numeric"))
-
 
258
}
-
 
259
 
-
 
260
setAs("track", "numeric", function(from) from@y)
-
 
261
 
-
 
262
t1 <- new("track", x=1:20, y=(1:20)^2)
-
 
263
 
-
 
264
as(t1, "numeric")
-
 
265
 
-
 
266
## The next example shows:
-
 
267
##  1. A virtual class to define setAs for several classes at once.
-
 
268
##  2. as() using inherited information
-
 
269
 
-
 
270
setClass("ca", slots = c(a = "character", id = "numeric"))
-
 
271
 
-
 
272
setClass("cb", slots = c(b = "character", id = "numeric"))
-
 
273
 
-
 
274
setClass("id")
-
 
275
setIs("ca", "id")
-
 
276
setIs("cb", "id")
-
 
277
 
81
 
-
 
82
\examples{
278
 
83
 
279
setAs("id", "numeric", function(from) from@id)
84
## Show all the existing methods for as()
-
 
85
showMethods("coerce")
280
 
86
 
281
CA <- new("ca", a = "A", id = 1)
-
 
282
CB <- new("cb", b = "B", id = 2)
-
 
-
 
87
}
283
 
88
 
-
 
89
\references{
284
setAs("cb", "ca", function(from, to )new(to, a=from@b, id = from@id))
90
 Chambers, John M. (2016)
-
 
91
 \emph{Extending R},
-
 
92
  Chapman & Hall.
-
 
93
(Chapters 9 and 10.)
-
 
94
}
285
 
95
 
286
as(CB, "numeric")
-
 
287
 
96
 
288
\dontshow{
-
 
289
## should generate an error (should have been a function of one argument)
-
 
290
try(setAs("track", "numeric", function(x, y,z)x@y))
-
 
291
}
-
 
292
}
-
 
293
\keyword{programming}
97
\keyword{programming}
294
\keyword{classes}
98
\keyword{classes}
295
\keyword{methods}
99
\keyword{methods}