[R] new.env() and attach for write?

Hadley Wickham h.wickham at gmail.com
Wed Aug 28 23:42:17 CEST 2013


On Wed, Aug 28, 2013 at 4:32 PM, ivo welch <ivo.welch at anderson.ucla.edu> wrote:
> is it possible to temporarily change the destination environment where
> objects are written to?  I am thinking
>
>   a <- new.env()
>   attach(a)
>   ### run some code, such as...
>   b <- function(x) x
>   detach(a)
>   a$b
>
> obviously, this is wrong.  attach() only attaches for read access.  I could
> copy the globalenv, run my code, see what objects have been changed (how?),
> move the changed and new functions into my a environment, and then restore
> globalenv.  or is this already done somewhere else?

within?

Or just:

evalq({
 b <- function(x) x
}, a)

Hadley

-- 
Chief Scientist, RStudio
http://had.co.nz/



More information about the R-help mailing list