Examples for 'base::bindenv'


Binding and Environment Locking, Active Bindings

Aliases: bindenv lockEnvironment environmentIsLocked lockBinding unlockBinding makeActiveBinding bindingIsLocked bindingIsActive activeBindingFunction

Keywords: utilities

### ** Examples

# locking environments
e <- new.env()
assign("x", 1, envir = e)
get("x", envir = e)
[1] 1
lockEnvironment(e)
get("x", envir = e)
[1] 1
assign("x", 2, envir = e)
try(assign("y", 2, envir = e)) # error
Error in assign("y", 2, envir = e) : 
  cannot add bindings to a locked environment
# locking bindings
e <- new.env()
assign("x", 1, envir = e)
get("x", envir = e)
[1] 1
lockBinding("x", e)
try(assign("x", 2, envir = e)) # error
Error in assign("x", 2, envir = e) : 
  cannot change value of locked binding for 'x'
unlockBinding("x", e)
assign("x", 2, envir = e)
get("x", envir = e)
[1] 2
# active bindings
f <- local( {
    x <- 1
    function(v) {
       if (missing(v))
           cat("get\n")
       else {
           cat("set\n")
           x <<- v
       }
       x
    }
})
makeActiveBinding("fred", f, .GlobalEnv)
bindingIsActive("fred", .GlobalEnv)
[1] TRUE
fred
get
[1] 1
fred <- 2
fred
[1] 2

[Package base version 4.2.3 Index]