Peter Waltman
2008-Jan-03 21:35 UTC
[R] Suggestion on how to make permanent changes to a single object in a list?
specifically, imagine we have: fooStack <- list() for ( i in 1:5 ) fooStack[[i]] <- list() and we have a function: fooModifier <- function( foo ) { foo$bar <- "bar" } then, if we invoke fooModifier, i.e.: fooModifier( fooStack[[ 1 ]] ) the $bar elt is only set in the scope of the function, and if we use the "<<-" modifier in fooModifier, R will throw an error b/c it can't find the "foo" object. I have to say that for someone coming from languages that have pointers and/or references, it's really frustrating that R fails to allow one to have direct access to the objects' memory space. Onyway, one workaround would be to pass in the whole fooStack object and the index of the elt that you want to modify to the fooModifier fn, but I'd rather not have to pass the whole thing in. Any suggestions? Thanks! Peter Waltman
Charilaos Skiadas
2008-Jan-03 21:45 UTC
[R] Suggestion on how to make permanent changes to a single object in a list?
You might want to consider using the proto package. Otherwise, functions that end in <- have the ability to alter their arguments. Look at the following (admittedly not very natural) construct: `fooModifier<-` <- function( foo, value ) { foo$bar <- "bar" } fooModifier( fooStack[[ 1 ]] ) <- NULL > fooStack [[1]] [1] "bar" [[2]] list() [[3]] list() [[4]] list() [[5]] list() I agree, that it would be nice to be able to do this more easily. Haris Skiadas Department of Mathematics and Computer Science Hanover College On Jan 3, 2008, at 4:35 PM, Peter Waltman wrote:> > specifically, imagine we have: > > fooStack <- list() > for ( i in 1:5 ) > fooStack[[i]] <- list() > > and we have a function: > > fooModifier <- function( foo ) { > > foo$bar <- "bar" > > } > > then, if we invoke fooModifier, i.e.: > > fooModifier( fooStack[[ 1 ]] ) > > the $bar elt is only set in the scope of the function, and if we > use the > "<<-" modifier in fooModifier, R will throw an error b/c it > can't find the > "foo" object. I have to say that for someone coming from > languages that > have pointers and/or references, it's really frustrating that R > fails to > allow one to have direct access to the objects' memory space. > Onyway, one workaround would be to pass in the whole fooStack > object and the > index of the elt that you want to modify to the fooModifier fn, > but I'd > rather not have to pass the whole thing in. > Any suggestions? > Thanks! > Peter Waltman
Gabor Grothendieck
2008-Jan-03 22:25 UTC
[R] Suggestion on how to make permanent changes to a single object in a list?
You can do it with environments. The first line sets up fooStack with a list of environments instead of a list of lists and the remaining lines are the same as in your post squished to one line each to make it easier to see the entire code at once: fooStack <- lapply(1:5, new.env) fooModifier <- function( foo ) foo$bar <- "bar" fooModifier( fooStack[[ 1 ]] ) fooStack[[1]]$bar # "bar" You may need to be a bit careful if you pursue this line of reasoning as there is a long standing bug in R relating to lists of promises so take care that you don't get promises in the list. See point #2 in: https://stat.ethz.ch/pipermail/r-devel/2008-January/047914.html Also you might want to look at the proto package which reframes the use of environments in terms of object oriented programming. http://r-proto.googlecode.com On Jan 3, 2008 4:35 PM, Peter Waltman <waltman at cs.nyu.edu> wrote:> > specifically, imagine we have: > > fooStack <- list() > for ( i in 1:5 ) > fooStack[[i]] <- list() > > and we have a function: > > fooModifier <- function( foo ) { > > foo$bar <- "bar" > > } > > then, if we invoke fooModifier, i.e.: > > fooModifier( fooStack[[ 1 ]] ) > > the $bar elt is only set in the scope of the function, and if we use the > "<<-" modifier in fooModifier, R will throw an error b/c it can't find the > "foo" object. I have to say that for someone coming from languages that > have pointers and/or references, it's really frustrating that R fails to > allow one to have direct access to the objects' memory space. > Onyway, one workaround would be to pass in the whole fooStack object and the > index of the elt that you want to modify to the fooModifier fn, but I'd > rather not have to pass the whole thing in. > Any suggestions? > Thanks! > Peter Waltman > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >