Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

locking - How to unlock environment in R?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...