### Name: new ### Title: Generate an Object from a Class ### Aliases: new initialize ### Keywords: programming classes ### ** Examples ## using the definition of class "track" from Classes ## Don't show: setClass("track", representation(x="numeric", y="numeric")) setClass("trackCurve", representation("track", smooth = "numeric")) ydata <- stats::rnorm(10); ysmooth <- 1:10 ## End Don't show ## 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. setMethod("initialize", "track", function(.Object, x = numeric(0), y = numeric(0)) { if(nargs() > 1) { if(length(x) != length(y)) stop("specified x and y of different lengths") .Object@x <- x .Object@y <- y } .Object }) ### the next example will cause an error (x will be numeric(0)), ### because we didn't build in defaults for x, ### although we could with a more elaborate method for initialize try(new("track", y = sort(stats::rnorm(10)))) ## a better way to implement the previous initialize method. ## Why? By using callNextMethod to call the default initialize method ## we don't inhibit classes that extend "track" from using the general ## form of the new() function. In the previous version, they could only ## use x and y as arguments to new, unless they wrote their own ## initialize method. setMethod("initialize", "track", function(.Object, ...) { .Object <- callNextMethod() if(length(.Object@x) != length(.Object@y)) stop("specified x and y of different lengths") .Object })