Changing the functional/global environment in R

江枫思渺然 提交于 2019-12-06 15:10:10

You are looking for eval / evalq

From help("evalq")

Evaluate an R expression in a specified environment.

Specifically, evalq noting

The evalq form is equivalent to eval(quote(expr), ...). eval evaluates its first argument in the current scope before passing it to the evaluator: evalq avoids this.

# Therefore you want something like this
evalq(C <- A + B, envir = other.env)

If you want to wrap more than one expression use {} eg

evalq({C <-A + B
    d <-  5
       }, envir = other.env)

How about using with?

original.env <- .GlobalEnv
other.env <- new.env() 
other.env$A <- 12; other.env$B <- 1.5
with(other.env, { C <- A + B ; FOO <- 'bar' })
other.env$C
[1] 13.5
other.env$FOO
[1] "bar"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!