How can I update the method definition of an R6 class instance?
S3 uses the current method defintion, as I would expect. With R5 (Reference Classes) I can use myInstance=myInstance$copy(). With R6 I tried myInstance = myInstance$clone() but myInstance$someMethod() still invokes the old code.
I need this, when I load object instances from a dump created on a long-running process. I want to debug and change the code on an object state after a long-running computation. Therefore, I cannot just create a new instance and rerun the initialization. Even better than the R5 copy method (which does not update references to the instance), would be a method that assigs the currently defined behaviour (i.e methods definitions) of the class and all super classes to the instance.
Here is an example:
library(R6)
Person <- R6Class("Person",
lock_objects=FALSE,
public = list(
name = NULL,
initialize = function(name = NA, hair = NA) {
self$name <- name
self$greet()
},
greet = function() {
cat(paste0("Hello, my name is ", self$name, ".\n"))
}
)
)
# create an instance
thomas <- Person$new("Thomas","brown")
# modify the behaviour of Person
Person <- R6Class("Person",
lock_objects=FALSE,
public = list(
name = NULL,
initialize = function(name = NA, hair = NA) {
self$name <- name
self$greet()
},
greet = function() {
cat(paste0("Modified greet from ", self$name, ".\n"))
}
)
)
t1 <- Person$new("t1") # greet function updated
t2 <- thomas$clone()
t2$greet() # greet function not updated in thomas nor t2