简体   繁体   中英

Reinstantiate an R6 object

Let's say I have a simple abstract R6 class.

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)
    }
  )
)

Then I have a child class which does some stuff

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_)
)

What I want to do is be able to save my created class and then reinstantiate it at a later time.

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)

The problem that I have is that for some reason self is not actually updated when we call $load() . If I debug the method, I see that it does get overwritten, but the object new still returns the initial myClass object without the loaded changes. The only way I can seem to get this to do what I want is by reassigning the output (obviously).

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

Now I understand that I can just readRDS() and be done with it but I want this to be chainable, hence trying to place this in a method.

active bindings can be used to call load directly.

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

such that a new parent can be placed in a chain

parent$new(tmpFile2)$file$addOne()
[1] 13

And a relevant caution from ?makeActiveBinding :

Currently active bindings are not preserved during package installation, but they can be created in.onLoad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM