I'd like to define two S4 classes, where one contains the other. The lower class has a particular slot, say x, and I need object@x to behave differently depending on whether object is the lower class or the wrapper class. For example:
setClass("foo",
contains = "numeric",
slots = c(x = "character"))
setClass("bar",
contains = "foo")
ff <- new("foo", 1:10, x = "abc")
bb <- new("bar", ff)
Now, from this I get:
> ff@x
[1] "abc"
> bb@x
[1] "abc"
What I'd like is when calling bb@x, to intercept the "abc" and modify it, such that bb@x produces something different than ff@x.
I tried setting a custom @ method for bar:
setMethod("@", signature(x = "bar"),
function(x, slot) {
out <- x@slot
if (slot == "x") {
out <- toupper(out)
}
out
})
but it fails with this error:
> Error in setGeneric(f, where = where) :
must supply a function skeleton for ‘@’, explicitly or via an existing function
I tried using method.skeleton, but this errors as well:
> method.skeleton("@", "bar")
Error in genericForBasic(name) :
methods may not be defined for primitive function ‘@’ in this version of R
Any other options?