| 42333 |
ripley |
1 |
% File src/library/methods/man/setClass.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2007 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 15357 |
jmc |
6 |
\name{setClass}
|
| 56186 |
murdoch |
7 |
\alias{setClass}
|
| 57843 |
jmc |
8 |
\alias{classGeneratorFunction-class}
|
| 15357 |
jmc |
9 |
\title{Create a Class Definition}
|
|
|
10 |
\description{
|
| 57843 |
jmc |
11 |
Create a class definition, specifying the representation (the
|
|
|
12 |
slots) and/or the classes contained in this one (the superclasses),
|
|
|
13 |
plus other optional details. As a side effect, the class definition
|
|
|
14 |
is stored in the specified environment. A generator function
|
|
|
15 |
is returned as the value of \code{setClass()}, suitable for creating
|
|
|
16 |
objects from the class if the class is not virtual. Of the many
|
|
|
17 |
arguments to the function only \code{Class},
|
| 62755 |
jmc |
18 |
\code{slots=} and \code{contains=} are usually needed.
|
| 19029 |
hornik |
19 |
}
|
| 15357 |
jmc |
20 |
\usage{
|
| 23531 |
hornik |
21 |
setClass(Class, representation, prototype, contains=character(),
|
| 48694 |
jmc |
22 |
validity, access, where, version, sealed, package,
|
| 61478 |
jmc |
23 |
S3methods = FALSE, slots)
|
| 15357 |
jmc |
24 |
}
|
|
|
25 |
\arguments{
|
| 45824 |
jmc |
26 |
\item{Class}{character string name for the class.}
|
| 61478 |
jmc |
27 |
\item{slots}{ a named list or named character vector.
|
|
|
28 |
The names are the names of the slots in the new class and
|
|
|
29 |
the elements are the character string names of the
|
|
|
30 |
corresponding classes.
|
| 45824 |
jmc |
31 |
|
| 61478 |
jmc |
32 |
In rare cases where there is ambiguity about the class of a slot,
|
|
|
33 |
because two classes of the same name are imported from different
|
|
|
34 |
packages, the corresponding element of the argument must have a
|
|
|
35 |
\code{"package"} attribute to disambiguate the choice.
|
|
|
36 |
|
|
|
37 |
It is allowed to provide an unnamed character vector as a limiting
|
|
|
38 |
case, with the elements taken as slot names and all slots having
|
|
|
39 |
the unrestricted class \code{"ANY"}.
|
|
|
40 |
|
|
|
41 |
}
|
|
|
42 |
\item{contains}{ the names (and optionally package slots) for the
|
|
|
43 |
\emph{superclasses} of this class. The special superclass
|
|
|
44 |
\code{"VIRTUAL"} causes the new class to be created as a
|
|
|
45 |
virtual class; see the section on virtual classes in \link{Classes}.}
|
| 45824 |
jmc |
46 |
\item{prototype}{ an object providing the default
|
| 57843 |
jmc |
47 |
data for the slots in this class. By default, each will be the
|
|
|
48 |
prototype object for the superclass. If provided, using a call to
|
|
|
49 |
\code{\link{prototype}} will carry out some checks. }
|
| 60963 |
maechler |
50 |
\item{where}{ the environment in which to store the definition.
|
|
|
51 |
Should not be supplied in standard use. For calls to
|
|
|
52 |
\code{setClass()} appearing in the source code for a package, will
|
|
|
53 |
default to the namespace of the package. For calls typed or sourced
|
|
|
54 |
at the top level in a session, will default to the global environment.
|
| 29714 |
ripley |
55 |
}
|
| 26365 |
jmc |
56 |
\item{validity}{ if supplied, should be a validity-checking method
|
| 29714 |
ripley |
57 |
for objects from this class (a function that returns \code{TRUE} if
|
|
|
58 |
its argument is a valid object of this class and one or more strings
|
|
|
59 |
describing the failures otherwise). See \code{\link{validObject}}
|
|
|
60 |
for details.}
|
| 61478 |
jmc |
61 |
\item{S3methods, representation, access, version }{ All these
|
|
|
62 |
arguments are deprecated from version 3.0.0 of \R and should be
|
|
|
63 |
avoided.
|
|
|
64 |
|
|
|
65 |
\code{S3methods} is a flag indicating that old-style methods
|
|
|
66 |
will be written involving this class. Modern versions of \R
|
|
|
67 |
attempt to match formal and old-style methods consistently, so
|
|
|
68 |
this argument is largely irrelevant.
|
|
|
69 |
|
|
|
70 |
\code{representation} is an argument inherited from S that
|
|
|
71 |
included both \code{slots} and \code{contains}, but the use of
|
|
|
72 |
the latter two arguments is clearer and recommended.
|
|
|
73 |
|
|
|
74 |
\code{access} and \code{version} are included for
|
| 57843 |
jmc |
75 |
historical compatibility with S-Plus, but ignored.}
|
| 45824 |
jmc |
76 |
\item{sealed}{ if \code{TRUE}, the class definition will be sealed,
|
| 21345 |
jmc |
77 |
so that another call to \code{setClass} will fail on this class name.
|
| 29714 |
ripley |
78 |
}
|
| 57843 |
jmc |
79 |
\item{package}{ an optional package name for the class. Should very
|
|
|
80 |
rarely be used. By default
|
|
|
81 |
the name of the package in which the class definition is assigned.
|
| 29714 |
ripley |
82 |
}
|
|
|
83 |
}
|
| 57843 |
jmc |
84 |
|
|
|
85 |
\value{
|
|
|
86 |
A generator function suitable for creating objects from the class is
|
|
|
87 |
returned, invisibly. A call to this function generates a call to
|
|
|
88 |
\code{\link{new}} for the class. The call takes any number of arguments,
|
|
|
89 |
which will be passed on to the initialize method. If no
|
|
|
90 |
\code{initialize} method is defined for the class or one of its
|
|
|
91 |
superclasses, the default method expects named arguments with the
|
|
|
92 |
name of one of the slots.
|
|
|
93 |
|
|
|
94 |
Typically the generator function is assigned the name of the class,
|
|
|
95 |
for programming clarity. This is not a requirement and objects
|
|
|
96 |
from the class can also be generated directly from
|
|
|
97 |
\code{\link{new}}. The advantages of the generator function are a
|
|
|
98 |
slightly simpler and clearer call, and that the call will contain
|
|
|
99 |
the package name of the class (eliminating any ambiguity if two
|
|
|
100 |
classes from different packages have the same name).
|
|
|
101 |
|
|
|
102 |
If the class is virtual, an attempt to generate an object from
|
|
|
103 |
either the generator or \code{new()}
|
|
|
104 |
will result in an error.
|
|
|
105 |
}
|
| 45824 |
jmc |
106 |
\section{Basic Use: Slots and Inheritance}{
|
| 57843 |
jmc |
107 |
The two essential arguments other than the class name are
|
| 61478 |
jmc |
108 |
\code{slots} and \code{contains}, defining the explicit slots
|
| 45824 |
jmc |
109 |
and the inheritance (superclasses). Together, these arguments define
|
|
|
110 |
all the information in an object from this class; that is, the names
|
|
|
111 |
of all the slots and the classes required for each of them.
|
| 25351 |
jmc |
112 |
|
| 45824 |
jmc |
113 |
The name of the class determines
|
| 51867 |
maechler |
114 |
which methods apply directly to objects from this class. The
|
| 45824 |
jmc |
115 |
inheritance information specifies which methods apply indirectly,
|
|
|
116 |
through inheritance. See \link{Methods}.
|
| 29714 |
ripley |
117 |
|
| 45824 |
jmc |
118 |
The slots in a class definition will be the union of all the slots
|
| 61478 |
jmc |
119 |
specified directly by \code{slots} and all the slots in all
|
| 45824 |
jmc |
120 |
the contained classes.
|
|
|
121 |
There can only be one slot with a given name; specifically, the
|
|
|
122 |
direct and inherited slot names must be unique.
|
|
|
123 |
That does not, however, prevent the same class from being inherited
|
|
|
124 |
via more than one path.
|
| 25351 |
jmc |
125 |
|
| 47935 |
jmc |
126 |
One kind of element in the \code{contains=} argument is special, specifying one of the \R
|
|
|
127 |
object types or one of a few other special \R types (\code{matrix} and
|
| 45824 |
jmc |
128 |
\code{array}).
|
| 47935 |
jmc |
129 |
See the section on inheriting from object types, below.
|
| 17000 |
jmc |
130 |
|
| 45824 |
jmc |
131 |
|
| 61478 |
jmc |
132 |
Slot names \code{"class"} and \code{"Class"} are not allowed.
|
| 51867 |
maechler |
133 |
There are other slot names with a special meaning; these names start with
|
| 47936 |
jmc |
134 |
the \code{"."} character. To be safe, you should define all of
|
| 47935 |
jmc |
135 |
your own slots with names starting with an alphabetic character.
|
| 15357 |
jmc |
136 |
}
|
|
|
137 |
|
| 57843 |
jmc |
138 |
|
|
|
139 |
|
| 47935 |
jmc |
140 |
\section{Inheriting from Object Types}{
|
|
|
141 |
In addition to containing other S4 classes, a class definition can
|
| 47936 |
jmc |
142 |
contain either an S3 class (see the next section) or a built-in R pseudo-class---one
|
| 47935 |
jmc |
143 |
of the \R
|
|
|
144 |
object types or one of the special \R pseudo-classes \code{"matrix"} and
|
| 48946 |
murdoch |
145 |
\code{"array"}.
|
| 47935 |
jmc |
146 |
A class can contain at most one of the object types, directly or indirectly.
|
|
|
147 |
When it does, that contained class determines the \dQuote{data part}
|
|
|
148 |
of the class.
|
| 15357 |
jmc |
149 |
|
| 47935 |
jmc |
150 |
Objects from the new class try to inherit the built in
|
|
|
151 |
behavior of the contained type.
|
|
|
152 |
In the case of normal \R data types, including vectors, functions and
|
|
|
153 |
expressions, the implementation is relatively straightforward.
|
|
|
154 |
For any object \code{x} from the class,
|
|
|
155 |
\code{typeof(x)} will be the contained basic type; and a special
|
|
|
156 |
pseudo-slot, \code{.Data}, will be shown with the corresponding class.
|
|
|
157 |
See the \code{"numWithId"} example below.
|
| 15357 |
jmc |
158 |
|
| 57843 |
jmc |
159 |
Classes may also inherit from \code{"vector"}, \code{"matrix"} or
|
|
|
160 |
\code{"array"}.
|
|
|
161 |
The data part of these objects can be any vector data type.
|
|
|
162 |
|
|
|
163 |
For an object from any class that does \emph{not} contain one of these
|
|
|
164 |
types or classes,
|
| 47935 |
jmc |
165 |
\code{typeof(x)} will be \code{"S4"}.
|
| 15357 |
jmc |
166 |
|
| 47935 |
jmc |
167 |
Some \R data types do not behave normally, in the sense that they are
|
|
|
168 |
non-local references or other objects that are not duplicated.
|
|
|
169 |
Examples include those corresponding to classes \code{"environment"}, \code{"externalptr"}, and \code{"name"}.
|
|
|
170 |
These can not be the types for objects with user-defined
|
|
|
171 |
classes (either S4 or S3) because setting an attribute overwrites the
|
|
|
172 |
object in all contexts.
|
|
|
173 |
It is possible to define a class that inherits from such types,
|
|
|
174 |
through an indirect mechanism that stores the inherited object in a
|
|
|
175 |
reserved slot.
|
| 57843 |
jmc |
176 |
See the
|
| 47936 |
jmc |
177 |
example for class \code{"stampedEnv"} below.
|
| 57843 |
jmc |
178 |
S3 method dispatch and the relevant \code{as.}\emph{type}\code{()}
|
|
|
179 |
functions should behave correctly, but code that uses the type of the
|
|
|
180 |
object directly will not.
|
| 47935 |
jmc |
181 |
|
| 57843 |
jmc |
182 |
Also, keep in mind that the object passed to low-level computations
|
| 47935 |
jmc |
183 |
will be the underlying object type, \emph{without} any of the slots
|
|
|
184 |
defined in the class.
|
|
|
185 |
To return the full information, you will usually have to define a
|
|
|
186 |
method that sets the data part.
|
|
|
187 |
|
| 31085 |
jmc |
188 |
}
|
| 29714 |
ripley |
189 |
|
| 47935 |
jmc |
190 |
\section{Inheriting from S3 Classes}{
|
|
|
191 |
Old-style S3 classes have no formal definition. Objects are
|
|
|
192 |
\dQuote{from} the class when their class attribute contains the
|
|
|
193 |
character string considered to be the class name.
|
| 31085 |
jmc |
194 |
|
| 47935 |
jmc |
195 |
Using such classes with formal classes and methods is necessarily a
|
|
|
196 |
risky business, since there are no guarantees about the content of the
|
|
|
197 |
objects or about consistency of inherited methods.
|
|
|
198 |
Given that, it is still possible to define a class that inherits from
|
|
|
199 |
an S3 class, providing that class has been registered as an old class
|
|
|
200 |
(see \code{\link{setOldClass}}).
|
|
|
201 |
|
| 57843 |
jmc |
202 |
Broadly speaking, both S3 and S4 method dispatch try to behave
|
|
|
203 |
sensibly with respect to inheritance in either system.
|
|
|
204 |
Given an S4 object, S3 method dispatch and the \code{\link{inherits}}
|
|
|
205 |
function should use the S4 inheritance information.
|
|
|
206 |
Given an S3 object, an S4 generic function will dispatch S4 methods
|
|
|
207 |
using the S3 inheritance, provided that inheritance has been declared via
|
|
|
208 |
\code{\link{setOldClass}}.
|
|
|
209 |
|
| 47935 |
jmc |
210 |
}
|
|
|
211 |
|
| 45824 |
jmc |
212 |
\section{Classes and Packages}{
|
| 31085 |
jmc |
213 |
|
| 45824 |
jmc |
214 |
Class definitions normally belong to packages (but can be defined in
|
|
|
215 |
the global environment as well, by evaluating the expression on the
|
|
|
216 |
command line or in a file sourced from the command line).
|
|
|
217 |
The corresponding package name is part of the class definition; that
|
|
|
218 |
is, part of the \code{classRepresentation} object holding that
|
|
|
219 |
definition. Thus, two classes with the same name can exist in
|
|
|
220 |
different packages, for most purposes.
|
| 31085 |
jmc |
221 |
|
| 57843 |
jmc |
222 |
When a class name is supplied for a slot or a superclass in a call to
|
|
|
223 |
\code{setClass}, a
|
| 45824 |
jmc |
224 |
corresponding class definition will be found, looking from the
|
| 57843 |
jmc |
225 |
namespace of the current package, assuming the call in question appears directly in the source for the
|
|
|
226 |
package, as it should to avoid ambiguity.
|
|
|
227 |
The class definition
|
|
|
228 |
must be found in the namespace of the current package, in the imports for that
|
|
|
229 |
namespace or in the basic classes defined by the methods package.
|
|
|
230 |
(The methods package must be included in the \code{Depends} directive
|
|
|
231 |
of the package's \code{"DESCRIPTION"} file in order for the
|
|
|
232 |
\code{"CMD check"} utility to find these classes.)
|
| 31085 |
jmc |
233 |
|
| 45824 |
jmc |
234 |
When this rule does not identify a class uniquely (because it appears
|
|
|
235 |
in more than one imported package) then the \code{\link{packageSlot}}
|
|
|
236 |
of the character string name needs to be supplied with the name.
|
|
|
237 |
This should be a rare occurrence.
|
| 37810 |
ripley |
238 |
}
|
|
|
239 |
|
| 15357 |
jmc |
240 |
\references{
|
| 45824 |
jmc |
241 |
Chambers, John M. (2008)
|
|
|
242 |
\emph{Software for Data Analysis: Programming with R}
|
|
|
243 |
Springer. (For the R version.)
|
| 15357 |
jmc |
244 |
|
| 45824 |
jmc |
245 |
Chambers, John M. (1998)
|
|
|
246 |
\emph{Programming with Data}
|
| 51867 |
maechler |
247 |
Springer (For the original S4 version.)
|
| 15357 |
jmc |
248 |
}
|
| 33556 |
maechler |
249 |
\seealso{
|
| 45824 |
jmc |
250 |
\code{\link{Classes}} for a general discussion of classes,
|
|
|
251 |
\code{\link{Methods}} for an analogous discussion of methods,
|
| 33556 |
maechler |
252 |
\code{\link{makeClassRepresentation}}
|
|
|
253 |
}
|
| 15357 |
jmc |
254 |
\examples{
|
| 26000 |
ripley |
255 |
\dontshow{
|
| 33353 |
maechler |
256 |
if(isClass("trackMultiCurve")) removeClass("trackMultiCurve")
|
| 47616 |
ripley |
257 |
if(isClass("trackCurve")) removeClass("trackCurve")
|
|
|
258 |
if(isClass("track")) removeClass("track")
|
| 15357 |
jmc |
259 |
}
|
|
|
260 |
## A simple class with two slots
|
| 57838 |
jmc |
261 |
track <- setClass("track",
|
| 61478 |
jmc |
262 |
slots = c(x="numeric", y="numeric"))
|
| 57843 |
jmc |
263 |
## an object from the class
|
|
|
264 |
t1 <- track(x = 1:10, y = 1:10 + rnorm(10))
|
|
|
265 |
|
| 15357 |
jmc |
266 |
## A class extending the previous, adding one more slot
|
| 57838 |
jmc |
267 |
trackCurve <- setClass("trackCurve",
|
| 61478 |
jmc |
268 |
slots = c(smooth = "numeric"),
|
| 45824 |
jmc |
269 |
contains = "track")
|
| 57843 |
jmc |
270 |
|
|
|
271 |
## an object containing a superclass object
|
|
|
272 |
t1s <- trackCurve(t1, smooth = 1:10)
|
|
|
273 |
|
| 15357 |
jmc |
274 |
## A class similar to "trackCurve", but with different structure
|
|
|
275 |
## allowing matrices for the "y" and "smooth" slots
|
| 23531 |
hornik |
276 |
setClass("trackMultiCurve",
|
| 61478 |
jmc |
277 |
slots = c(x="numeric", y="matrix", smooth="matrix"),
|
| 23531 |
hornik |
278 |
prototype = list(x=numeric(), y=matrix(0,0,0),
|
|
|
279 |
smooth= matrix(0,0,0)))
|
| 57843 |
jmc |
280 |
## See ?setIs for further examples using these classes
|
| 17376 |
jmc |
281 |
|
| 31085 |
jmc |
282 |
## A class that extends the built-in data type "numeric"
|
|
|
283 |
|
| 61478 |
jmc |
284 |
numWithId <- setClass("numWithId", slots = c(id = "character"),
|
| 33353 |
maechler |
285 |
contains = "numeric")
|
| 31085 |
jmc |
286 |
|
| 57838 |
jmc |
287 |
numWithId(1:3, id = "An Example")
|
| 31085 |
jmc |
288 |
|
| 47936 |
jmc |
289 |
## inherit from reference object of type "environment"
|
| 57838 |
jmc |
290 |
stampedEnv <-setClass("stampedEnv", contains = "environment",
|
| 61478 |
jmc |
291 |
slots = c(update = "POSIXct"))
|
| 47936 |
jmc |
292 |
setMethod("[[<-", c("stampedEnv", "character", "missing"),
|
|
|
293 |
function(x, i, j, ..., value) {
|
|
|
294 |
ev <- as(x, "environment")
|
|
|
295 |
ev[[i]] <- value #update the object in the environment
|
|
|
296 |
x@update <- Sys.time() # and the update time
|
|
|
297 |
x})
|
|
|
298 |
|
| 57838 |
jmc |
299 |
|
|
|
300 |
e1 <- stampedEnv(update = Sys.time())
|
|
|
301 |
|
| 47936 |
jmc |
302 |
e1[["noise"]] <- rnorm(10)
|
|
|
303 |
|
| 26000 |
ripley |
304 |
\dontshow{
|
| 15357 |
jmc |
305 |
tMC <- new("trackMultiCurve")
|
|
|
306 |
is.matrix(slot(tMC, "y"))
|
|
|
307 |
is.matrix(slot(tMC, "smooth"))
|
|
|
308 |
setClass("myMatrix", "matrix", prototype = matrix(0,0,0))
|
|
|
309 |
nrow(new("myMatrix")) # 0
|
|
|
310 |
nrow(new("matrix")) # 1
|
| 17007 |
jmc |
311 |
## simple test of prototype data
|
| 41508 |
ripley |
312 |
xxx <- stats::rnorm(3)
|
| 61478 |
jmc |
313 |
setClass("xNum", slots = c(x = "numeric"), prototype = list(x = xxx))
|
| 17007 |
jmc |
314 |
stopifnot(identical(new("xNum")@x, xxx))
|
| 21853 |
jmc |
315 |
|
| 57838 |
jmc |
316 |
removeClass("xNum")
|
|
|
317 |
removeClass("myMatrix")
|
| 21853 |
jmc |
318 |
|
| 18126 |
jmc |
319 |
## The following should not be needed. But make check removes all files
|
|
|
320 |
## between example files, in a crude way that does not cause the class
|
|
|
321 |
## information to be reset. There seems no way to detect this, so we
|
|
|
322 |
## have to remove classes ourselves
|
|
|
323 |
|
| 23531 |
hornik |
324 |
removeClass("trackMultiCurve")
|
|
|
325 |
removeClass("trackCurve")
|
|
|
326 |
removeClass("track")
|
| 33556 |
maechler |
327 |
}%dont show
|
| 15357 |
jmc |
328 |
}
|
|
|
329 |
\keyword{programming}
|
|
|
330 |
\keyword{classes}
|
|
|
331 |
\keyword{methods}
|