解压缩 R 环境对象的内容到当前工作环境

huangapple go评论60阅读模式
英文:

"unpack" contents of an R environment object to current working environment

问题

assign("a", env$a)
assign("b", "bar")
assign("c", env$c)
英文:

I want to achieve an effect similar to saving and loading .RData files like the following code, but without writing anything out to a file, and using environments instead.

a <- 1
c <- 3
save('a', 'c', file="file.RData")
a <- 'foo'
b <- 'bar'
load("file.RData")

So lets say I have some variables stored within an environment, some which share names of variables in the working environment

a <- 'foo'
b <- 'bar'
env <- new.env()
env$a <- 1
env$c <- 3

I want to unpack all the contents of env into the current environment, possibly overwriting some variables, such that the final values of each variable are

a = 1
b = 'bar'
c = 3

答案1

得分: 4

env 创建一个列表,然后使用 list2env

list2env(as.list(env), .GlobalEnv)

或者等效地使用管道

env |> as.list() |> list2env(.GlobalEnv)
英文:

Create a list from env and then use list2env

list2env(as.list(env), .GlobalEnv)

or equivalently as a pipeline

env |> as.list() |> list2env(.GlobalEnv)

huangapple
  • 本文由 发表于 2023年4月20日 01:01:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定