I think I now understand how R looks up names. Could anyone tell me if I have this right? First it looks up the nested environments created by lexical scoping. Then, if it gets to the top (.GlobalEnv) it also looks through the list of things that have been "attach"ed. It never looks in the call stack unless you explicitly ask it to, or mess with the environment frames. The reason I ask is that it's not entirely clear to me from the "R Language Definition" how these 3 search spaces (environments/lexical scoping; call stack/dynamic scoping; attach/search list) are related. For example the discussion of 3.5.3 ("the call stack") observes that dynamic scoping "contradicts the default scoping rules in R". I spent some time trying to figure out how it could do both, before deciding it doesn't. I suppose the implicit corollary of the contradiction referred to in 3.5.3--"so we don't do that and you must intervene to achieve dynamic scoping"--was obvious to the authors. It just wasn't obvious to me. Since I'm still not sure, I thought I'd check. Thanks. -- Ross Boylan wk: (415) 502-4031 530 Parnassus Avenue (Library) rm 115-4 ross at biostat.ucsf.edu Dept of Epidemiology and Biostatistics hm: (415) 550-1062 University of California, San Francisco San Francisco, CA 94143-0840
On 3 Jun 2003, Ross Boylan wrote:> I think I now understand how R looks up names. Could anyone tell me if > I have this right? > > First it looks up the nested environments created by lexical scoping. > Then, if it gets to the top (.GlobalEnv) it also looks through the list > of things that have been "attach"ed. > > It never looks in the call stack unless you explicitly ask it to, or > mess with the environment frames. > > The reason I ask is that it's not entirely clear to me from the "R > Language Definition" how these 3 search spaces (environments/lexical > scoping; call stack/dynamic scoping; attach/search list) are related. > For example the discussion of 3.5.3 ("the call stack") observes that > dynamic scoping "contradicts the default scoping rules in R". I spent > some time trying to figure out how it could do both, before deciding it > doesn't. I suppose the implicit corollary of the contradiction referred > to in 3.5.3--"so we don't do that and you must intervene to achieve > dynamic scoping"--was obvious to the authors. It just wasn't obvious to > me. Since I'm still not sure, I thought I'd check. >Yes. -thomas
On Tue, 2003-06-03 at 16:34, Robert Gentleman wrote:> Also, note that you can get the effect of lexical scope by doing > things like >Do you mean "you can get the effects of dynamic scope...."?> f<- function(x) x+y > e1 <- new.env() > assign("y", 10, env=e1) > environment(f) <- e1 > > #now like lexical scope; you can futz with f's environment, assigning, > # modifying as you like >P.S. Thanks to everyone who responded. So fast!