How to restore the previous function after active binding in R

Viewed 23

I want to rebind the system call to a default function such as

# override system()
env <- as.environment("package:base")
unlockBinding("system", env) # bindings in the base R are write-protected
assign(
  "system",
  function(...){stop("This is a forbidden command!")},
  envir=env
)
lockBinding("system", env)

but after that, I want to restore the previous function system(), how can I do? If I choose to use the following code, is there a solution?

# override system()
env <- as.environment("package:base")
unlockBinding("system", env) # bindings in the base R are write-protected
makeActiveBinding("system", function(...){stop("This is a forbidden command!")}, env)
lockBinding("system", env)
0 Answers
Related