R: Use active binding in object generator to conditionally add new class to R6 objects

半腔热情 提交于 2021-02-18 14:42:48

问题


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.


回答1:


You can change the class of self.

library(R6)

thing <- R6Class(
  "youngThing",
  private = list(..age = 0),
  active = list(
    age = function() {
      private$..age <- private$..age + 1

      if(private$..age > 5 && !inherits(self, "olderThing")){
        class(self) <- c("olderThing", class(self))
      }
      private$..age
    }
  )
)

a_thing has it's original class while age <= 5.

a_thing <- thing$new()

a_thing$age; a_thing$age; a_thing$age; a_thing$age; a_thing$age
#> [1] 2
#> [1] 3
#> [1] 4
#> [1] 5

class(a_thing)
#> [1] "youngThing" "R6" 

Then it updates once it goes over 5.

a_thing$age
#> [1] 6

class(a_thing)
#> [1] "olderThing" "youngThing" "R6" 


来源:https://stackoverflow.com/questions/64597394/r-use-active-binding-in-object-generator-to-conditionally-add-new-class-to-r6-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!