## from setClass
## A simple class with two slots
setClass("track",
representation(x="numeric", y="numeric"))
## A class extending the previous, adding one more slot
setClass("trackCurve",
representation("track", smooth = "numeric"))
## A class similar to "trackCurve", but with different structure
## allowing matrices for the "y" and "smooth" slots
setClass("trackMultiCurve",
representation(x="numeric", y="matrix", smooth="matrix"),
prototype = list(x=numeric(), y=matrix(0,0,0), smooth= matrix(0,0,0)))
setIs("trackMultiCurve", "trackCurve",
test = function(obj) {ncol(slot(obj, "y")) == 1},
coerce = function(obj) { new("trackCurve", x = slot(obj, "x"),
y = as.numeric(slot(obj,"y")), smooth = as.numeric(slot(obj, "smooth")))})
## from setMethod
require(graphics)
setMethod("plot", "track",
function(x, y, ...) plot(slot(x, "y"), y, ...)
)
setMethod("plot", c("trackCurve", "missing"),
function(x, y, ...) {
plot(as(x, "track"))
if(length(slot(x, "smooth") > 0))
lines(slot(x, "x"), slot(x, "smooth"))
}
)
promptClass("trackMultiCurve", stdout())
promptClass("track", stdout())
> promptClass("track")
A shell of class documentation has been written to the
file "track-class.Rd".
removeMethods("plot")
Run the code above in your browser using DataLab