## 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.
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
})
Run the code above in your browser using DataLab