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()
TNX, in advance
TNX,提前
5 个解决方案
#1
22
There seem to be at least 3 different things you can do:
你可以做至少3种不同的事情:
- Clone an environment (create an exact duplicate)
- 克隆环境(创建完全重复)
- Copy the content of one environment to another environment
- 将一个环境的内容复制到另一个环境
- Share the same environment
- 共享相同的环境
To clone:
要克隆:
# Make the source env
e1 <- new.env()
e1$foo <- 1
e1$.bar <- 2 # a hidden name
ls(e1) # only shows "foo"
# This will clone e1
e2 <- as.environment(as.list(e1, all.names=TRUE))
# Check it...
identical(e1, e2) # FALSE
e2$foo
e2$.bar
To copy the content, you can do what @gsk showed. But again, the all.names
flag is useful:
要复制内容,您可以执行@gsk显示的内容。但同样,all.names标志很有用:
# e1 is source env, e2 is dest env
for(n in ls(e1, all.names=TRUE)) assign(n, get(n, e1), e2)
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:
分享环境是@koshke所做的。这可能通常更有用。结果与创建本地函数的结果相同:
f2 <- function() {
v1 <- 1
v2 <- 2
# This local function has access to v1 and v2
flocal <- function() {
print(v1)
print(v2)
}
return(flocal)
}
f1 <- f2()
f1() # prints 1 and 2
#2
9
Try this:
尝试这个:
f2 <- function() {
v1 <- 1
v2 <- 2
environment(f1) <<- environment()
}
#3
5
You could use assign:
您可以使用assign:
f1 <- function() {
print(v1)
print(v2)
}
f2 <- function() {
v1 <- 1
v2 <- 2
for(obj in c("v1","v2")) {
assign(obj,get(obj),envir=f1.env)
}
}
If you don't want to list out the objects, ls()
takes an environment argument.
如果您不想列出对象,则ls()接受环境参数。
And you'll have to figure out how to get f1.env to be an environment pointing inside f1 :-)
你必须弄清楚如何让f1.env成为一个指向f1的环境:-)
#4
2
I use this function in my package to copy objects:
我在我的包中使用此函数来复制对象:
copyEnv <- function(from, to, names=ls(from, all.names=TRUE)) {
mapply(assign, names, mget(names, from), list(to),
SIMPLIFY = FALSE, USE.NAMES = FALSE)
invisible(NULL)
}
#5
1
To do it:
去做吧:
environment(f1) <- environment(f2) # It does not work
Open the f1
environment and run do this:
打开f1环境并运行:
ls(load(f2))
#1
22
There seem to be at least 3 different things you can do:
你可以做至少3种不同的事情:
- Clone an environment (create an exact duplicate)
- 克隆环境(创建完全重复)
- Copy the content of one environment to another environment
- 将一个环境的内容复制到另一个环境
- Share the same environment
- 共享相同的环境
To clone:
要克隆:
# Make the source env
e1 <- new.env()
e1$foo <- 1
e1$.bar <- 2 # a hidden name
ls(e1) # only shows "foo"
# This will clone e1
e2 <- as.environment(as.list(e1, all.names=TRUE))
# Check it...
identical(e1, e2) # FALSE
e2$foo
e2$.bar
To copy the content, you can do what @gsk showed. But again, the all.names
flag is useful:
要复制内容,您可以执行@gsk显示的内容。但同样,all.names标志很有用:
# e1 is source env, e2 is dest env
for(n in ls(e1, all.names=TRUE)) assign(n, get(n, e1), e2)
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:
分享环境是@koshke所做的。这可能通常更有用。结果与创建本地函数的结果相同:
f2 <- function() {
v1 <- 1
v2 <- 2
# This local function has access to v1 and v2
flocal <- function() {
print(v1)
print(v2)
}
return(flocal)
}
f1 <- f2()
f1() # prints 1 and 2
#2
9
Try this:
尝试这个:
f2 <- function() {
v1 <- 1
v2 <- 2
environment(f1) <<- environment()
}
#3
5
You could use assign:
您可以使用assign:
f1 <- function() {
print(v1)
print(v2)
}
f2 <- function() {
v1 <- 1
v2 <- 2
for(obj in c("v1","v2")) {
assign(obj,get(obj),envir=f1.env)
}
}
If you don't want to list out the objects, ls()
takes an environment argument.
如果您不想列出对象,则ls()接受环境参数。
And you'll have to figure out how to get f1.env to be an environment pointing inside f1 :-)
你必须弄清楚如何让f1.env成为一个指向f1的环境:-)
#4
2
I use this function in my package to copy objects:
我在我的包中使用此函数来复制对象:
copyEnv <- function(from, to, names=ls(from, all.names=TRUE)) {
mapply(assign, names, mget(names, from), list(to),
SIMPLIFY = FALSE, USE.NAMES = FALSE)
invisible(NULL)
}
#5
1
To do it:
去做吧:
environment(f1) <- environment(f2) # It does not work
Open the f1
environment and run do this:
打开f1环境并运行:
ls(load(f2))