I would like to ask if it is possible to copy/move all the objects of one environment to another, at once. For example:
f1 <- function() {
print(v1)
print(v2)
}
f2 <- function() {
v1 <- 1
v2 <- 2
# environment(f1)$v1 <- v1 # It works
# environment(f1)$v2 <- v2 # It works
environment(f1) <- environment(f2) # It does not work
}
f2()
f1()
There seem to be at least 3 different things you can do:
To clone:
To copy the content, you can do what @gsk showed. But again, the
all.namesflag is useful:To share the environment is what @koshke did. This is probably often much more useful. The result is the same as if creating a local function: