Hi, In order to avoid deep copies by passing large arguments to functions or returning values, I'm trying to do the assignment of variables in a given environment. The problem is when I try to assign a structure: a list for example. If I have: ind <- c("a","b") my idea is doing something like l <- alist() l[ind] <- as.list(c(20,40)) in a given environment. Example: ref <- new.env() (.....) assign("l",alist(),env=ref) If I do assign("l$a",20,env=ref) it creates me a new variable in the ref environment named "l$a" So, I did: eval(l <- alist(), env=ref) but this creates the l list both on the current and on the ref environment. The alternative solution that I found out was: evalq(l<-alist(),env=ref) and then evalq(ind <-c("a","b"), env=ref) evalq(l[ind] <- as.list(c(20,40)), env=ref) I would like to know if there is another possible solution, instead of doing these 'evalqs' along the program code. Thanks, Rita -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, In order to avoid deep copies by passing large arguments to functions or returning values, I'm trying to do the assignment of variables in a given environment. The problem is when I try to assign a structure: a list for example. If I have: ind <- c("a","b") my idea is doing something like l <- alist() l[ind] <- as.list(c(20,40)) in a given environment. Example: ref <- new.env() (.....) assign("l",alist(),env=ref) If I do assign("l$a",20,env=ref) it creates me a new variable in the ref environment named "l$a" So, I did: eval(l <- alist(), env=ref) but this creates the l list both on the current and on the ref environment. The alternative solution that I found out was: evalq(l<-alist(),env=ref) and then evalq(ind <-c("a","b"), env=ref) evalq(l[ind] <- as.list(c(20,40)), env=ref) I would like to know if there is another possible solution, instead of doing these 'evalqs' along the program code. Thanks, Rita -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David A Richmond
2001-Oct-16 22:39 UTC
[R] Assignment of structures on a given environment
I was having trouble with a similar issue recently, until I was informed that arguments to functions aren't copied unless they're altered. So I can pass a large matrix through a routine with as many sublevels as i need and the matrix won't be recreated everytime I pass it to another function. Is this the problem you're trying to avoid? dave On Tue, 16 Oct 2001, Rita Ribeiro wrote:> Hi, > > In order to avoid deep copies by passing large arguments to functions or > returning values, I'm trying to do the assignment of variables in a > given environment. The problem is when I try to assign a structure: a > list for example. > > If I have: > ind <- c("a","b") > > my idea is doing something like > > l <- alist() > l[ind] <- as.list(c(20,40)) > > in a given environment. > > > > Example: > ref <- new.env() > (.....) > assign("l",alist(),env=ref) > > > If I do > assign("l$a",20,env=ref) > > it creates me a new variable in the ref environment named "l$a" > > > > So, I did: > > eval(l <- alist(), env=ref) > > but this creates the l list both on the current and on the ref > environment. > > > The alternative solution that I found out was: > > evalq(l<-alist(),env=ref) > > and then > > evalq(ind <-c("a","b"), env=ref) > evalq(l[ind] <- as.list(c(20,40)), env=ref) > > > I would like to know if there is another possible solution, instead of > doing these 'evalqs' along the program code. > > > Thanks, > > Rita > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |David Richmond It works on a | + Dept. of Sociology complex scientific + |Saint Mary's College principle, known as | + Notre Dame, IN 46556 "pot luck." + |219-284-4517 - The Doctor | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, I've already read something about it, and I known that when passing arguments to functions, R only passes a "promise" . The argument is copied only if its value is changed. But the problem is that I'm sucessively building a list along the functions calls, so the value is always updated.... Rita David A Richmond wrote:> I was having trouble with a similar issue recently, until I wasinformed> that arguments to functions aren't copied unless they're altered. So Ican> pass a large matrix through a routine with as many sublevels as i needand> the matrix won't be recreated everytime I pass it to another function.Is> this the problem you're trying to avoid? > > dave > > On Tue, 16 Oct 2001, Rita Ribeiro wrote: > > > Hi, > > > > In order to avoid deep copies by passing large arguments tofunctions or> > returning values, I'm trying to do the assignment of variables in a > > given environment. The problem is when I try to assign a structure:a> > list for example. > > > > If I have: > > ind <- c("a","b") > > > > my idea is doing something like > > > > l <- alist() > > l[ind] <- as.list(c(20,40)) > > > > in a given environment. > > > > > > > > Example: > > ref <- new.env() > > (.....) > > assign("l",alist(),env=ref) > > > > > > If I do > > assign("l$a",20,env=ref) > > > > it creates me a new variable in the ref environment named "l$a"> > > > > > > > So, I did: > > > > eval(l <- alist(), env=ref) > > > > but this creates the l list both on the current and on the ref > > environment. > > > > > > The alternative solution that I found out was: > > > > evalq(l<-alist(),env=ref) > > > > and then > > > > evalq(ind <-c("a","b"), env=ref) > > evalq(l[ind] <- as.list(c(20,40)), env=ref) > > > > > > I would like to know if there is another possible solution, insteadof> > doing these 'evalqs' along the program code. > > > > > > Thanks, > > > > Rita > > > > > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> > r-help mailing list -- Readhttp://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html> > Send "info", "help", or "[un]subscribe" > > (in the "body", not the subject !) To:r-help-request at stat.math.ethz.ch> >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._> > > > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > |David Richmond It works on a | > + Dept. of Sociology complex scientific + > |Saint Mary's College principle, known as | > + Notre Dame, IN 46556 "pot luck." + > |219-284-4517 - The Doctor | > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> r-help mailing list -- Readhttp://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html> Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To:r-help-request at stat.math.ethz.ch>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, Robert Gentleman wrote:> Yes there is a way without evalq. > Simply get and retrieve the list. From your description you say that > you need to > 1) keep the list structure > 2) keep successively updating the list structure > > >From those perspectives putting the list in an environment will not > help you save copies. In R we generally copy on mutate (not always and > it could be improved but mostly). > > You really have not described the problem that you are trying to > solve, rather you are giving us an abstraction and from that it is >The list I'm building keeps a variety of information about some dataset! Among other things the list keeps the dataset itself in the data frame R format. As there are some huge datasets, I was wishing to avoid frequent copies of that structure. I've already pass that list as an argument and there seems to be no problem in the execution. The only issue is that in very large datasets those frequent copies could slow down the execution. I've also stored this list in the Global environment, but as the program produces also an output file with similar information I wouldn't like the idea of leaving some "garbage" in the Global environment or making users manage some global variable...... Ok, I can do 'rm', but if users do want to have access to the produced list?? My idea was making the program return some kind of pointer to the environment where is the list at, which users would use if they want to access it.... I thought that way it wouldn't be necessary any copies.... Well, I'm seeing that there is no much way around it.... Thanks again for the tips, Rita> very hard for anyone to give you sensible advice. > > I still truely doubt that size is an issue. Have you tried passing the > list directly? Do you know that size is an issue or do you just > presume it? > > Finally a very ugly solution that involves no evalq's > > You can always store your list in the Global environment and access it > using get, assign and <<-. There should be only one copy but if you > keep changing it, it will be duplicated, that's kind of necessary. > > -- > +---------------------------------------------------------------------------+ > | Robert Gentleman phone : (617) 632-5250 | > | Associate Professor fax: (617) 632-2444 | > | Department of Biostatistics office: M1B28 > | Harvard School of Public Health email: rgentlem at jimmy.dfci.harvard.edu | > +---------------------------------------------------------------------------+-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._