Suppose you have one S4 class "A" and a subclass of "B", which has additional features. Each of them has its own validation checks - B should only check additional functions. Now, when initializing B, I would like to start with an object of class A, and then change it with additional functions. However, this creates problems, and I assume that I am violating R's assumptions somewhere in this example.
Here is the dummy code:
setClass(Class="A",
representation=
representation(x="numeric"),
validity=
function(object){stopifnot(x > 0)})
setMethod("initialize",
signature(.Object="A"),
function(.Object,
...,
z){
x <- get("z") + 1
callNextMethod(.Object,
...,
x=x)
})
setClass(Class="B",
contains="A",
representation=
representation(y="numeric"),
validity=
function(object){stopifnot(y > 0)})
setMethod("initialize",
signature(.Object="B"),
function(.Object,
...,
bla){
.Object <- callNextMethod(.Object,
...)
.Object@y <- .Object@x + bla
return(.Object)
})
test <- new("B",
z=4,
bla=5)
If I try to create a test object, I get:
Error in stopifnot(x > 0): object 'x' not found
Do you know how I could do better?
Many thanks! Regards Daniel