Thursday, February 23, 2012

create variables in a created environment inside global environment inside a function

# create an environment inside the global 
# environment and create variables inside
# that environment 

# ref: http://goo.gl/GCQhZ
foofun <- function() {
   assign("env1", new.env(parent = globalenv()),  envir = globalenv()) 

   # this would not work 
   # env1 <- new.env(parent = globalenv());
   assign("tmpv", "tmpv", envir = globalenv()$env1) 
} 

foofun() 

ls() 

ls(envir = globalenv()$env1) 

rm("env1", envir = globalenv()) 
ls() 

#

No comments: