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

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

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

问题

  1. assign("a", env$a)
  2. assign("b", "bar")
  3. 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.

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

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

  1. a <- 'foo'
  2. b <- 'bar'
  3. env <- new.env()
  4. env$a <- 1
  5. 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

  1. a = 1
  2. b = 'bar'
  3. c = 3

答案1

得分: 4

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

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

或者等效地使用管道

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

Create a list from env and then use list2env

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

or equivalently as a pipeline

  1. 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:

确定