重新实例化 R6 对象

nat*_*ood 8 r r6

假设我有一个简单的抽象R6类。

myClass <- R6::R6Class(
  classname = "myClass",
  public = list(
    save = function(path) {
      saveRDS(self, path)
    },
    load = function(path) {
      object <- readRDS(path)
      self <- object
      lockEnvironment(self)
      invisible(self)
    }
  )
)
Run Code Online (Sandbox Code Playgroud)

然后我有一个儿童班可以做一些事情

myChildClass <- R6::R6Class(
  classname = "myChildClass",
  inherit = myClass,
  lock_objects = FALSE,
  public = list(
    initialize = function(x) {
      private$x <- x
    },
    addOne = function() {
      private$x <- private$x + 1
      private$x
    }
  ),
  private = list(x = NA_real_)
)
Run Code Online (Sandbox Code Playgroud)

我想要做的是能够保存我创建的类,然后在以后重新实例化它。

tmp <- myChildClass$new(x = 10)
tmp$addOne()
tmp$addOne()
tmpFile <- tempfile()
tmp$save(tmpFile)
new <- myClass$new()
new$load(tmpFile)
new
# <myClass>
#   Public:
#     clone: function (deep = FALSE)
#     load: function (path)
#     save: function (path)
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,由于某种原因self,当我们调用$load(). 如果我调试该方法,我会发现它确实被覆盖,但该对象new仍然返回初始myClass对象,而没有加载更改。我似乎可以让它做我想做的事情的唯一方法是重新分配输出(显然)。

new <- myClass$new()
new <- new$load(tmpFile)
new
# <myChildClass>
#   Inherits from: <myClass>
#   Public:
#     addOne: function ()
#     clone: function (deep = FALSE) 
#     initialize: function (x)
#     load: function (path)
#     save: function (path)
#   Private:
#     x: 12
Run Code Online (Sandbox Code Playgroud)

现在我明白我可以readRDS()完成它,但我希望它是可链接的,因此尝试将其放入一个方法中。

Don*_*nen 0

可以使用主动绑定load直接调用。

parent <- R6::R6Class(
    classname = "parent",
    public = list(
        # update path if supplied in new()
        initialize = function(path = NULL) if(!is.null(path)) self$path <- path,
        path = NULL,
        save = function(path) {
            saveRDS(self, path)
        },
        load = function(path) {
            object <- readRDS(path)
            self <- object
            lockEnvironment(self)
            invisible(self)
        }
    ),
    active = list(
        file = function() self <- self$load(self$path) # computed at each access
    )
)
child <- R6::R6Class(
    classname = "child",
    inherit = parent,
    lock_objects = FALSE,
    public = list(
        initialize = function(x) {
            private$x <- x
        },
        addOne = function() {
            private$x <- private$x + 1
            private$x
        }
    ),
    private = list(x = NA_real_)
)

tmp1 <- child$new(x = 10)
tmp1$addOne() # 11
tmpFile <- tempfile() #tmp nr 1
tmp1$save(tmpFile)

tmp2 <- parent$new(tmpFile)$file
tmp2$addOne() # 12
tmpFile2 <- tempfile()
tmp2$save(tmpFile2)

tmp3 <- parent$new(tmpFile2)$file
tmp3

#> <child>
#>   Inherits from: <parent>
#>   Public:
#>     addOne: function () 
#>     clone: function (deep = FALSE) 
#>     file: active binding
#>     initialize: function (x) 
#>     load: function (path) 
#>     path: NULL
#>     save: function (path) 
#>   Private:
#>     x: 12
Run Code Online (Sandbox Code Playgroud)

这样就可以将新的父级放入链中

parent$new(tmpFile2)$file$addOne()
[1] 13
Run Code Online (Sandbox Code Playgroud)

以及以下相关警告?makeActiveBinding

当前活动绑定在包安装过程中不会保留,但可以在 .onLoad 中创建。