# NOT RUN {
setClass("track",
slots = c(x="numeric", y = "numeric"))
t1 <- new("track", x=1:10, y=sort(stats::rnorm(10)))
## A valid "track" object has the same number of x, y values
validTrackObject <- function(object) {
if(length(object@x) == length(object@y)) TRUE
else paste("Unequal x,y lengths: ", length(object@x), ", ",
length(object@y), sep="")
}
## assign the function as the validity method for the class
setValidity("track", validTrackObject)
## t1 should be a valid "track" object
validObject(t1)
## Now we do something bad
t2 <- t1
t2@x <- 1:20
## This should generate an error
# }
# NOT RUN {
try(validObject(t2))
# }
# NOT RUN {
setClass("trackCurve", contains = "track",
slots = c(smooth = "numeric"))
## all superclass validity methods are used when validObject
## is called from initialize() with arguments, so this fails
# }
# NOT RUN {
trynew("trackCurve", t2)
# }
# NOT RUN {
setClass("twoTrack", slots = c(tr1 = "track", tr2 ="track"))
## validity tests are not applied recursively by default,
## so this object is created (invalidly)
tT <- new("twoTrack", tr2 = t2)
## A stricter test detects the problem
# }
# NOT RUN {
try(validObject(tT, complete = TRUE))
# }
Run the code above in your browser using DataLab