[R] Superassignment (<<-) and indexing

Thomas Lumley tlumley at u.washington.edu
Sat Sep 3 00:23:49 CEST 2005

On Fri, 2 Sep 2005, Spencer Graves wrote:

> 	  Permit a mild protest on the word "appropriate" in this context.  The
> global assignment operator "<<-" provides, for my tastes, excessive
> opportunities for problems.  If I define "x" someplace else and then
> call your function, it may change my "x" in ways that generate
> considerable wailing and gnashing of teeth.

No, no, no.

The sensible and appropriate uses of <<- involve modifying a variable that 
already exists in the lexical parent environment.  In these cases it can't 
escape and ravage the calling environment.

Certainly using <<- to assign to the calling environment is bogus.  In 
addition to your complaints, it doesn't even work (except from the global 
environment), since <<- searches the lexical stack rather than the call 

In R, <<- can be used safely to maintain state inside a function or shared 
between a set of functions (as in demo(scoping), or demo(tkdensity)). In 
S-PLUS it is admittedly harder to come up with good uses.


More information about the R-help mailing list