Wednesday, 15 January 2014

Lazy evaluation in R – is assign affected? -



Lazy evaluation in R – is assign affected? -

i read this basic question on renaming objects , @shane 's reply it, pointing me lazy evaluation. wonder if assign evaluated lazily, too. here:

assign("somenewname",someoldobject) rm(someoldobject)

the reason why wonder next utilize case: assume got 10k+ r objects each of has 2 attributes called originalname , additionalname. want write function can efficiently allow user switch 1 name other without losing these 2 attributes. this...

edit: based on @hadley's input have changed code.

switchobjectname <- function(x) { n1 <- attributes(x)$originalname n2 <- attributes(x)$additionalname objname <- deparse(substitute(x)) if(objname == n1) { delayedassign(n2,x,assign.env=.globalenv) } else { delayedassign(n1,x,assign.env=.globalenv) } rm(list=c(objname),envir=.globalenv) }

this works well, had quite problem rm statement right. tried rm(objname,envir=.globalenv) not work though objname character cause result of deparse(substitute(x).

r lazy-evaluation assign

No comments:

Post a Comment