I have a simple R6 object generator:
thing <- R6Class("youngThing",
private = list(
..age = 0),
active = list(
age = function(){
private$..age <- private$..age + 1
private$..age
}
)
)
That gives me a simple R6 object, where ..age
increases by 1 every time the active age
field is called:
a_thing <- thing$new()
a_thing$age
# [1] 1
I want the object class of a_thing to change given a threshold value of the private field ..age
, like this:
class(a_thing)
# [1] "youngThing" "R6"
for(timestep in 1:10){
if(a_thing$age >5 & ! inherits(a_thing, "olderThing")){
class(a_thing) <- c("olderThing", class(a_thing))
}
}
class(a_thing)
# [1] "olderThing" "youngThing" "R6"
However, I want this to happen within the object. Is there a way to include this as an active binding in the object generator, so that any object created from it will have this functionality built-in?
NB. It is preferred that the threshold class is added to the object; that it does not replace the existing classes.