Skip to content
Prev 33073 / 63424 Next

V2.9.0 changes [Sec=Unclassified]

Troy Robertson wrote:
ah good, if you're frustrated with Java you'll find R very different ;)
The object inherits slots from it's parent, and the methods defined on
the parent class. Maybe this example helps?

setClass("Ticker", contains=".environment")

## initialize essential, so each instance gets its own environment
setMethod(initialize, "Ticker",
          function(.Object, ..., .xData=new.env(parent=emptyenv()))
{
    .xData[["count"]] <- 0
    callNextMethod(.Object, ..., .xData=.xData)
})

## tick: increment (private) counter by n
setGeneric("tick", function(reference, n=1L) standardGeneric("tick"),
           signature="reference")

setMethod(tick, "Ticker", function(reference, n=1L) {
    reference[["count"]] <- reference[["count"]] + n
})

## tock: report current value of counter
setGeneric("tock", function(reference) standardGeneric("tock"))

setMethod(tock, "Ticker", function(reference) {
    reference[["count"]]
})

and then
[1] 0
[1] 11
[1] 11
[1] 12

The data inside .environment could be structured, too, using S4.
Probably it would be more appropriate to have the environment as a slot,
rather the class that is being extended. And in terms of inherited
'properties', e.g., the "[[" function as defined on environments is
available
Of course I haven't seen your code, but a different interpretation of
your performance issues is that, within the rules of S4, you've chosen
to implement functionality in an inefficient way. So it might be
instructive to step back a bit and try to reformulate your data
structures and methods. This is hard to do.

Martin