17

Playing with Binding and Environment Adjustments in R , we have this 3 functions:

  1. lockEnvironment(env) locks env so you can't add a new symbol to env.
  2. lockBinding(sym, env) locks the sym within env so you can't modfiy it
  3. unlockBinding(sym, env) relax the latter lock.

But how can I Unlock the environment? Maybe I miss something but it looks like R don't expose an unlockEnvironment function or equivalent mechanism to unlock the env ? Is there some design reason to this?

Here an example of how to use this functions:

e <- new.env()
lockEnvironment(e)
get("x",e)
assign("x",2,envir=e)
lockBinding("x", e)
get("x",e)
unlockBinding("x", e)
assign("x",3,envir=e)

## how to relese e lock?
unlockEnvironment(e) ## the function doesn't exist
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 1
    I see neither a robust answer nor any apparent addition of `unlockEnvironment()` as of at least R-3.5.1. Have you found any resolution or explanation? – r2evans Feb 21 '19 at 16:29
  • see also https://gist.github.com/wch/3280369#file-unlockenvironment-r and https://stackoverflow.com/questions/25910778/unlockenvironment-implemented-via-rcpp-instead-of-inline/25922051#25922051 – moodymudskipper Nov 27 '19 at 09:09

2 Answers2

4

I think the best you can do is make a new unlocked environment. You could either copy all the fields, or make the existing one a parent of the new one. That means all the existing variables get inherited.

unlockEnvironment <- function (env) {
  return (new.env(parent=env))
}

e <- unlockEnvironment(e)
Steve Pitchers
  • 7,088
  • 5
  • 41
  • 41
0

The {rlang} package provides env_unlock() for development usage:

rlang::env_unlock(env)
Quasimodo
  • 172
  • 10