[R] environments

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Tue Aug 26 15:16:56 CEST 2008


Douglas Bates wrote:
> As Henrique said, the canonical way of assigning a value within an
> environment is the "assign".  A more obscure, but also more effective,
> approach is evalq which quotes an expression then evaluates it in the
> given environment.  For example
>
>   
>> env <- new.env()
>> evalq({aa <- 1:3; bb <- LETTERS[1:9]; cc <- list(A = aa, B = bb)}, env)
>> objects(env)
>>     
> [1] "aa" "bb" "cc"
>   
>> env$aa
>>     
> [1] 1 2 3
>   

Yes, and the with() construct works similarly. You do have to be careful
to note that also the right hand side of the assignment is evaluated in
env. This can have unexpected consequences.

Notice also the possibility of lexical scoping and superassignment
"<<-". See for instance

file.show(system.file("demo/scoping.R", package="base"))


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list