I want to define a function in an environment, which uses globals from this environment:
e <- new.env()
e$a <- 1
e$f <- function() a
## e$f()
## Error in e$f() : object 'a' not found
environment(e$f) <- e
e$f()
# [1] 1
So far so good. Now assume I add another (locked) binding to e:
e$b <- 2
lockBinding("b", e)
How could I (temporarily) unlock this binding from within f without using the symbol e?
e$f <- function() unlockBinding("b", parent.frame())
environment(e$f) <- e
e$f()
does not work because parent.frame does not properly point to e.
I do not want to use e directly because this would break as soon as I clone environments:
e <- new.env()
e$a <- e$b <- 1
lockBinding("b", e)
e$f <- function() unlockBinding("b", e)
environment(e$f) <- e
## e$b <- 3
## Error in e$b <- 3 : cannot change value of locked binding for 'b'
g <- rlang::env_clone(e)
g$f()
e$b <- 3 ## should not work