[R] Assignment of structures on a given environment

Robert Gentleman rgentlem at jimmy.harvard.edu
Tue Oct 16 18:45:17 CEST 2001


On Tue, Oct 16, 2001 at 04:40:34PM +0100, 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"
> 
  This is what you asked it to do.
  Environments work at the level of whole objects. I'm not sure what
  it would mean for l$a to exist in ref if l doesn't exist in ref.
  So, you just assign l into the environment and retrieve it from the
  environment, using get.

  If you you have only one list then perhaps you do not want to put
  the list in, rather just the elements.

   names <- names(l)
   for(i in 1:length(l)) assign(names[i], l[[i]], env=ref)

   you then have a and b, for example, in the environment and can
   extract them.
  
   If you have two lists, l1 and l2 and both have a's and b's as
   components then you have to put in the whole list (or adopt a
   naming convention).

   One might ask whether you are sure that you need to do this?
   I work on very large data sets and copying has pretty much never
   been an issue.

> 
> 
>     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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
+---------------------------------------------------------------------------+
| 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list