Mark Heckmann
2012-Mar-07 16:51 UTC
[R] transfer local function objects to other environment - how?
Hello, I have an empty environment named env. Now I want the locally created objects in some function (foo) to appear in env. Within the function I want to have straight forward code, no assign operation or env$x etc. in front of every command. What is the best way to do that? Example: foo <- function(){ x <- 1 y <- 2 } env <- new.env() Now I want to evaluate foo() but have the local variables x and y appear in env. Thanks ---Mark ???????????????????????????????????? Mark Heckmann Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com
Gabor Grothendieck
2012-Mar-07 17:00 UTC
[R] transfer local function objects to other environment - how?
On Wed, Mar 7, 2012 at 11:51 AM, Mark Heckmann <mark.heckmann at gmx.de> wrote:> Hello, > > I have an empty environment named env. > Now I want the locally created objects in some function (foo) to appear in env. > Within the function I want to have straight forward code, no assign operation or env$x etc. in front of every command. > What is the best way to do that? > > Example: > > foo <- function(){ > ?x <- 1 > ?y <- 2 > } > env <- new.env() >Try: foo <- function(){ x <- 1 y <- 2 environment() } e <- foo() or you could just forget about the function and do: e <- local({ x <- 1 y <- 2 environment() }) or e <- as.environment(list(x = 1, y = 2)) or library(proto) p <- proto(x = 1, y = 2) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com