[R] transfer local function objects to other environment - how?

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 7 18:00:05 CET 2012


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



More information about the R-help mailing list