In R, we can use makeActiveBinding
to create an active binding by supplying a function:
e <- new.env()
makeActiveBinding("var",
function(x) if (missing(x)) cat("get\n") else cat("set\n"), e)
Once the active binding is created, there seems no way of getting the function behind it.
I wonder if it is possible to get the function behind the active binding like the following?
> getActiveBindingFunction("var", e)
function(x) if (missing(x)) cat("get\n") else cat("set\n"), e)
Since R 4.0.0 you can directly use activeBindingFunction()
. It's not really documented but is straightforward enough!
e <- new.env()
makeActiveBinding("var",
function(x) if (missing(x)) cat("get\n") else cat("set\n"), e)
activeBindingFunction("var", e)
#> function(x) if (missing(x)) cat("get\n") else cat("set\n")
Answer goes out to hrbrmstr. You can coerce the environment to the list then access the function. Please see the code below:
e <- new.env()
makeActiveBinding("var", function(x) if (missing(x)) cat("get\n") else cat("set\n"), e)
as.list(e)$var
Ouput:
function(x) if (missing(x)) cat("get\n") else cat("set\n")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With