EVOLUTION-MANAGER
Edit File: new.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Generate an Object from a Class</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for new {methods}"><tr><td>new {methods}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Generate an Object from a Class </h2> <h3>Description</h3> <p>A call to <code>new</code> returns a newly allocated object from the class identified by the first argument. This call in turn calls the method for the generic function <code>initialize</code> corresponding to the specified class, passing the <code>...</code> arguments to this method. In the default method for <code>initialize()</code>, named arguments provide values for the corresponding slots and unnamed arguments must be objects from superclasses of this class. </p> <p>A call to a generating function for a class (see <code><a href="setClass.html">setClass</a></code>) will pass its ... arguments to a corresponding call to <code>new()</code>. </p> <h3>Usage</h3> <pre> new(Class, ...) initialize(.Object, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>Class</code></td> <td> <p>either the name of a class, a <code><a href="../../base/html/character.html">character</a></code> string, (the usual case) or the object describing the class (e.g., the value returned by <code>getClass</code>). Note that the character string passed from a generating function includes the package name as an attribute, avoiding ambiguity if two packages have identically named classes.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>arguments to specify properties of the new object, to be passed to <code>initialize()</code>.</p> </td></tr> <tr valign="top"><td><code>.Object</code></td> <td> <p> An object: see the “Initialize Methods” section.</p> </td></tr> </table> <h3>Initialize Methods</h3> <p>The generic function <code>initialize</code> is not called directly. A call to <code>new</code> begins by copying the prototype object from the class definition, and then calls <code>intialize()</code> with this object as the first argument, followed by the ... arguments. </p> <p>The interpretation of the <code>...</code> arguments in a call to a generator function or to <code>new()</code> can be specialized to particular classes, by defining an appropriate method for <code>"initialize"</code>. </p> <p>In the default method, unnamed arguments in the <code>...</code> are interpreted as objects from a superclass, and named arguments are interpreted as objects to be assigned into the correspondingly named slots. Explicitly specified slots override inherited information for the same slot, regardless of the order in which the arguments appear. </p> <p>The <code>initialize</code> methods do not have to have <code>...</code> as their second argument (see the examples). Initialize methods are often written when the natural parameters describing the new object are not the names of the slots. If you do define such a method, you should include <code>...</code> as a formal argument, and your method should pass such arguments along via <code><a href="NextMethod.html">callNextMethod</a></code>. This helps the definition of future subclasses of your class. If these have additional slots and your method does <em>not</em> have this argument, it will be difficult for these slots to be included in an initializing call. </p> <p>See <code><a href="initialize-methods.html">initialize-methods</a></code> for a discussion of some classes with existing methods. </p> <p>Methods for <code>initialize</code> can be inherited only by simple inheritance, since it is a requirement that the method return an object from the target class. See the <code>simpleInheritanceOnly</code> argument to <code><a href="setGeneric.html">setGeneric</a></code> and the discussion in <code><a href="setIs.html">setIs</a></code> for the general concept. </p> <p>Note that the basic vector classes, <code>"numeric"</code>, etc. are implicitly defined, so one can use <code>new</code> for these classes. The ... arguments are interpreted as objects of this type and are concatenated into the resulting vector. </p> <h3>References</h3> <p>Chambers, John M. (2016) <em>Extending R</em>, Chapman & Hall. (Chapters 9 and 10.) </p> <h3>See Also</h3> <p><a href="Classes_Details.html">Classes_Details</a> for details of class definitions, and <code><a href="setOldClass.html">setOldClass</a></code> for the relation to S3 classes. </p> <h3>Examples</h3> <pre> ## using the definition of class "track" from \link{setClass} ## a new object with two slots specified t1 <- new("track", x = seq_along(ydata), y = ydata) # a new object including an object from a superclass, plus a slot t2 <- new("trackCurve", t1, smooth = ysmooth) ### define a method for initialize, to ensure that new objects have ### equal-length x and y slots. In this version, the slots must still be ### supplied by name. setMethod("initialize", "track", function(.Object, ...) { .Object <- callNextMethod() if(length(.Object@x) != length(.Object@y)) stop("specified x and y of different lengths") .Object }) ### An alternative version that allows x and y to be supplied ### unnamed. A still more friendly version would make the default x ### a vector of the same length as y, and vice versa. setMethod("initialize", "track", function(.Object, x = numeric(0), y = numeric(0), ...) { .Object <- callNextMethod(.Object, ...) if(length(x) != length(y)) stop("specified x and y of different lengths") .Object@x <- x .Object@y <- y .Object }) </pre> <hr /><div style="text-align: center;">[Package <em>methods</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>