| Line 1... |
Line 1... |
| 1 |
% File src/library/methods/man/new.Rd
|
1 |
% File src/library/methods/man/new.Rd
|
| 2 |
% Part of the R package, https://www.R-project.org
|
2 |
% Part of the R package, https://www.R-project.org
|
| 3 |
% Copyright 1995-2016 R Core Team
|
3 |
% Copyright 1995-2017 R Core Team
|
| 4 |
% Distributed under GPL 2 or later
|
4 |
% Distributed under GPL 2 or later
|
| 5 |
|
5 |
|
| 6 |
\name{new}
|
6 |
\name{new}
|
| 7 |
\alias{new}
|
7 |
\alias{new}
|
| 8 |
\alias{initialize}
|
8 |
\alias{initialize}
|
| 9 |
\title{ Generate an Object from a Class }
|
9 |
\title{ Generate an Object from a Class }
|
| 10 |
\description{
|
10 |
\description{
|
| - |
|
11 |
A call to \code{new} returns a newly allocated object from the
|
| 11 |
Given the name or the definition of a class, plus optionally data
|
12 |
class identified by the first argument. This call in turn calls the
|
| - |
|
13 |
method for the generic function \code{initialize} corresponding to
|
| 12 |
to be included in the object, \code{new} returns an object from that
|
14 |
the specified class, passing the \code{\dots} arguments to this
|
| 13 |
class.
|
15 |
method.
|
| - |
|
16 |
In the default method for \code{initialize()}, named arguments provide
|
| - |
|
17 |
values for the corresponding slots and unnamed arguments must be
|
| - |
|
18 |
objects from superclasses of this class.
|
| - |
|
19 |
|
| - |
|
20 |
A call to a generating function for a class (see
|
| - |
|
21 |
\code{\link{setClass}}) will pass its \dots arguments to a corresponding call to \code{new()}.
|
| 14 |
}
|
22 |
}
|
| 15 |
\usage{
|
23 |
\usage{
|
| 16 |
new(Class, ...)
|
24 |
new(Class, ...)
|
| 17 |
|
25 |
|
| 18 |
initialize(.Object, ...)
|
26 |
initialize(.Object, ...)
|
| 19 |
}
|
27 |
}
|
| 20 |
\arguments{
|
28 |
\arguments{
|
| 21 |
\item{Class}{either the name of a class, a \code{\link{character}}
|
29 |
\item{Class}{either the name of a class, a \code{\link{character}}
|
| 22 |
string, (the usual case) or the object describing the class (e.g.,
|
30 |
string, (the usual case) or the object describing the class (e.g.,
|
| 23 |
the value returned by \code{getClass}).}
|
31 |
the value returned by \code{getClass}). Note that the character
|
| 24 |
\item{\dots}{data to include in the new object. Named arguments
|
32 |
string passed from a generating function includes the package name
|
| 25 |
correspond to slots in the class definition. Unnamed arguments must
|
33 |
as an attribute, avoiding ambiguity if two packages have identically
|
| - |
|
34 |
named classes.}
|
| - |
|
35 |
\item{\dots}{arguments to specify properties of the new object, to
|
| 26 |
be objects from classes that this class extends.}
|
36 |
be passed to \code{initialize()}.}
|
| 27 |
\item{.Object}{ An object: see the Details section.}
|
37 |
\item{.Object}{ An object: see the \dQuote{Initialize Methods} section.}
|
| 28 |
}
|
38 |
}
|
| 29 |
\details{
|
39 |
\section{Initialize Methods}{
|
| 30 |
The function \code{new} begins by copying the prototype object from
|
- |
|
| 31 |
the class definition. Then information is inserted according to the
|
40 |
The generic function \code{initialize} is not called directly.
|
| 32 |
\code{\dots} arguments, if any. As of version 2.4 of R, the type of
|
41 |
A call to \code{new} begins by copying the prototype object from
|
| 33 |
the prototype object, and therefore of all objects returned by
|
42 |
the class definition, and then calls \code{intialize()} with this
|
| 34 |
\code{new()}, is \code{"S4"} except for classes that extend
|
- |
|
| 35 |
one of the basic types, where the prototype has that basic type. User
|
43 |
object as the first argument, followed by the \dots{} arguments.
|
| 36 |
functions that depend on \code{\link{typeof}(object)} should be
|
- |
|
| 37 |
careful to handle \code{"S4"} as a possible type.
|
- |
|
| 38 |
|
44 |
|
| 39 |
Note that the \emph{name} of the first argument, \code{"Class"}
|
45 |
The interpretation of the \code{\dots} arguments in a call to a
|
| 40 |
entails that \code{"Class"} is an undesirable slot name in any formal
|
- |
|
| 41 |
class: \code{new("myClass", Class = <value>)} will not work.
|
- |
|
| 42 |
|
- |
|
| 43 |
The interpretation of the \code{\dots} arguments can be specialized to
|
46 |
generator function or to \code{new()} can be specialized to
|
| 44 |
particular classes, if an appropriate method has been defined for the
|
47 |
particular classes, by defining an appropriate method for \code{"initialize"}.
|
| 45 |
generic function \code{"initialize"}. The \code{new} function calls
|
- |
|
| 46 |
\code{initialize} with the object generated from the prototype as the
|
- |
|
| 47 |
\code{.Object} argument to \code{initialize}.
|
- |
|
| 48 |
|
48 |
|
| 49 |
By default, unnamed arguments in the \code{\dots} are interpreted as
|
49 |
In the default method, unnamed arguments in the \code{\dots} are interpreted as
|
| 50 |
objects from a superclass, and named arguments are interpreted as
|
50 |
objects from a superclass, and named arguments are interpreted as
|
| 51 |
objects to be assigned into the correspondingly named slots. Thus,
|
51 |
objects to be assigned into the correspondingly named slots.
|
| 52 |
explicit slots override inherited information for the same slot,
|
52 |
Explicitly specified slots override inherited information for the same slot,
|
| 53 |
regardless of the order in which the arguments appear.
|
53 |
regardless of the order in which the arguments appear.
|
| 54 |
|
54 |
|
| 55 |
The \code{initialize} methods do not have to have \code{\dots} as
|
55 |
The \code{initialize} methods do not have to have \code{\dots} as
|
| 56 |
their second argument (see the examples). Initialize methods are
|
56 |
their second argument (see the examples). Initialize methods are
|
| 57 |
often written when the natural parameters describing the new object
|
57 |
often written when the natural parameters describing the new object
|
| 58 |
are not the names of the slots. If you do define such a method,
|
58 |
are not the names of the slots. If you do define such a method,
|
| 59 |
note the implications for future subclasses of your class. If these
|
- |
|
| 60 |
have additional slots, and your \code{initialize} method has
|
- |
|
| 61 |
\code{\dots} as a formal argument, then your method should pass such
|
59 |
you should include \code{\dots} as a formal argument, and your method should pass such
|
| 62 |
arguments along via \code{\link{callNextMethod}}. If your method
|
60 |
arguments along via \code{\link{callNextMethod}}.
|
| 63 |
does not have this argument, then either a subclass must have its
|
61 |
This helps the definition of future subclasses of your class. If these
|
| - |
|
62 |
have additional slots and your method
|
| 64 |
own method or else the added slots must be specified by users in
|
63 |
does \emph{not} have this argument, it will be difficult for these
|
| 65 |
some way other than as arguments to \code{new}.
|
64 |
slots to be included in an initializing call.
|
| 66 |
|
65 |
|
| 67 |
For examples of \code{initialize} methods, see
|
66 |
See
|
| 68 |
\code{\link{initialize-methods}} for existing methods for
|
67 |
\code{\link{initialize-methods}} for a discussion of some classes with existing
|
| 69 |
classes \code{"traceable"} and \code{"environment"}, among
|
- |
|
| 70 |
others. See the comments there on subclasses of
|
- |
|
| 71 |
\code{"environment"}; any \code{initialize} methods for these should
|
- |
|
| 72 |
be sure to allocate a new environment.
|
68 |
methods.
|
| 73 |
|
69 |
|
| 74 |
Methods for \code{initialize} can be inherited only by simple
|
70 |
Methods for \code{initialize} can be inherited only by simple
|
| 75 |
inheritance, since it is a requirement that the method return an
|
71 |
inheritance, since it is a requirement that the method return an
|
| 76 |
object from the target class. See the
|
72 |
object from the target class. See the
|
| 77 |
\code{simpleInheritanceOnly} argument to \code{\link{setGeneric}} and
|
73 |
\code{simpleInheritanceOnly} argument to \code{\link{setGeneric}} and
|
| 78 |
the discussion in \code{\link{setIs}} for the general concept.
|
74 |
the discussion in \code{\link{setIs}} for the general concept.
|
| 79 |
|
75 |
|
| 80 |
Note that the basic vector classes, \code{"numeric"}, etc. are
|
76 |
Note that the basic vector classes, \code{"numeric"}, etc. are
|
| 81 |
implicitly defined, so one can use \code{new} for these classes.
|
77 |
implicitly defined, so one can use \code{new} for these classes.
|
| - |
|
78 |
The \dots arguments are interpreted as objects of this type and are
|
| - |
|
79 |
concatenated into the resulting vector.
|
| 82 |
}
|
80 |
}
|
| 83 |
\references{
|
81 |
\references{
|
| 84 |
Chambers, John M. (2008)
|
82 |
Chambers, John M. (2016)
|
| 85 |
\emph{Software for Data Analysis: Programming with R}
|
83 |
\emph{Extending R},
|
| 86 |
Springer. (For the R version.)
|
84 |
Chapman & Hall.
|
| 87 |
|
- |
|
| 88 |
Chambers, John M. (1998)
|
85 |
(Chapters 9 and 10.)
|
| 89 |
\emph{Programming with Data}
|
- |
|
| 90 |
Springer (For the original S4 version.)
|
- |
|
| 91 |
}
|
86 |
}
|
| 92 |
\seealso{ \link{Classes_Details} for details of class definitions, and
|
87 |
\seealso{ \link{Classes_Details} for details of class definitions, and
|
| 93 |
\code{\link{setOldClass}} for the relation to S3 classes. }
|
88 |
\code{\link{setOldClass}} for the relation to S3 classes. }
|
| 94 |
|
89 |
|
| 95 |
\examples{
|
90 |
\examples{
|
| Line 108... |
Line 103... |
| 108 |
|
103 |
|
| 109 |
# a new object including an object from a superclass, plus a slot
|
104 |
# a new object including an object from a superclass, plus a slot
|
| 110 |
t2 <- new("trackCurve", t1, smooth = ysmooth)
|
105 |
t2 <- new("trackCurve", t1, smooth = ysmooth)
|
| 111 |
|
106 |
|
| 112 |
### define a method for initialize, to ensure that new objects have
|
107 |
### define a method for initialize, to ensure that new objects have
|
| - |
|
108 |
### equal-length x and y slots. In this version, the slots must still be
|
| 113 |
### equal-length x and y slots.
|
109 |
### supplied by name.
|
| 114 |
|
110 |
|
| 115 |
setMethod("initialize",
|
111 |
setMethod("initialize", "track",
|
| - |
|
112 |
function(.Object, ...) {
|
| - |
|
113 |
.Object <- callNextMethod()
|
| - |
|
114 |
if(length(.Object@x) != length(.Object@y))
|
| - |
|
115 |
stop("specified x and y of different lengths")
|
| 116 |
"track",
|
116 |
.Object
|
| - |
|
117 |
})
|
| - |
|
118 |
|
| - |
|
119 |
### An alternative version that allows x and y to be supplied
|
| - |
|
120 |
### unnamed. A still more friendly version would make the default x
|
| - |
|
121 |
### a vector of the same length as y, and vice versa.
|
| - |
|
122 |
|
| - |
|
123 |
setMethod("initialize", "track",
|
| 117 |
function(.Object, x = numeric(0), y = numeric(0)) {
|
124 |
function(.Object, x = numeric(0), y = numeric(0), ...) {
|
| 118 |
if(nargs() > 1) {
|
125 |
.Object <- callNextMethod(.Object, ...)
|
| 119 |
if(length(x) != length(y))
|
126 |
if(length(x) != length(y))
|
| 120 |
stop("specified x and y of different lengths")
|
127 |
stop("specified x and y of different lengths")
|
| 121 |
.Object@x <- x
|
128 |
.Object@x <- x
|
| 122 |
.Object@y <- y
|
129 |
.Object@y <- y
|
| 123 |
}
|
- |
|
| 124 |
.Object
|
130 |
.Object
|
| 125 |
})
|
131 |
})
|
| 126 |
|
132 |
|
| 127 |
### the next example will cause an error (x will be numeric(0)),
|
- |
|
| 128 |
### because we didn't build in defaults for x,
|
- |
|
| 129 |
### although we could with a more elaborate method for initialize
|
- |
|
| 130 |
|
- |
|
| 131 |
try(new("track", y = sort(stats::rnorm(10))))
|
- |
|
| 132 |
|
- |
|
| 133 |
## a better way to implement the previous initialize method.
|
- |
|
| 134 |
## Why? By using callNextMethod to call the default initialize method
|
- |
|
| 135 |
## we don't inhibit classes that extend "track" from using the general
|
- |
|
| 136 |
## form of the new() function. In the previous version, they could only
|
- |
|
| 137 |
## use x and y as arguments to new, unless they wrote their own
|
- |
|
| 138 |
## initialize method.
|
133 |
\dontshow{
|
| 139 |
|
- |
|
| 140 |
setMethod("initialize", "track", function(.Object, ...) {
|
134 |
removeMethod("initialize", "track")
|
| 141 |
.Object <- callNextMethod()
|
- |
|
| 142 |
if(length(.Object@x) != length(.Object@y))
|
- |
|
| 143 |
stop("specified x and y of different lengths")
|
- |
|
| 144 |
.Object
|
- |
|
| 145 |
})
|
- |
|
| 146 |
|
135 |
}
|
| 147 |
}
|
136 |
}
|
| 148 |
\keyword{programming}
|
137 |
\keyword{programming}
|
| 149 |
\keyword{classes}
|
138 |
\keyword{classes}
|