Closed iferres closed 5 years ago
Dynamically change an object's fields seems unintuitive (at least to me).
Of course, it's possible by hacking (see the example code below) but I suggest create a field which is an environment and bind functions there...
proto <- R6::R6Class(
active = list(
f1 = function() rnorm(1),
f2 = function() rnorm(2)
),
public = list(
rm_bindings = function(x) {
unlockBinding(x, env = self)
makeActiveBinding(x, function() NULL, env = self)
lockBinding(x, self)
}
)
)
obj <- proto$new()
obj$f1
#> [1] -0.7540934
obj$f2
#> [1] 0.3585274 -1.2203444
obj$rm_bindings('f1')
obj$f1
#> NULL
obj$f2
#> [1] -1.0596080 0.8594399
Created on 2019-03-10 by the reprex package (v0.2.1)
I think @iferres is talking about removing something from the class (proto
in your example), not from an instance of the class (obj
). @iferres is that correct?
Thanks both @shrektan and @wch , for your answers. Yes, I was asking how to remove something from the class, as @wch have pointed out.
Similar hack...
proto <- R6::R6Class(
active = list(
f1 = function() rnorm(1),
f2 = function() rnorm(2)
)
)
obj <- proto$new()
obj$f1
#> [1] -1.128665
obj$f2
#> [1] 0.9919912 0.5682875
proto$active$f1 <- NULL
obj2 <- proto$new()
obj2$f1
#> NULL
obj2$f2
#> [1] 2.007603 -1.158038
Created on 2019-03-16 by the reprex package (v0.2.1)
I tried this but I don't see possible to access $active
. All active bindings are set as public, and I can't set an active binding to NULL
(throws error). What I would like to achieve is to "reset" a class with default methods/fields/bindings (i.e. with only those which the class comes out of package, without the ones manually added after loading it), may be there's a better way to accomplish that.
I'm using R6 version 2.4.0.
I don’t understand what you mean. Can you run the example code above? Or you can, but you can’t change a r6class from a loaded R package ? Or?
Oh, sorry, I misunderstood your argument. It works as expected and solve my problem. Thanks!
Hi, quick question. I was wondering if there's any way to remove an active binding (or any type of field) using something like
MyR6Class$set()
(but the opposite). In the same way I can add functions/fields/bindings, I would like to remove them if needed. I tried setting a previously added active binding toNULL
but it didn't work. Thanks!