Skip to content

Superassignment (<<-) and indexing

5 messages · Brahm, David, Thomas Lumley, Spencer Graves

#
In a clean environment under R-2.1.0 on Linux:
Error: Object "x" not found

Isn't that odd?  (Note x <<- 9 works just fine.)

Why am I doing this?  Because I'm stepping through code that
normally lives inside a function, where "<<-" is appropriate.

-- David Brahm (brahm at alum.mit.edu)
#
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.  Unless I assign the 
function output to "x", then the action of your function will change my 
"x" in ways I did not anticipate, possibly generating many problems for 
me later -- with extreme difficulties in finding the source of the 
problem.  Moreover, if your library expects to later find in "x" what 
your function stored there, there could be other problems, because I 
might redefine "x" before you use it.  The library might work fine when 
you use it but not for someone else -- and tracing the problem can be 
difficult.

	  I understand that "<<-" may allow your function f1 to call f2 and 
have f2 change "x" in f1.  However, if your f2 gets called some other 
way or if the name of "x" is misspelled or changed in either f1 or f2, 
we could be back to the situation I just described.

	  spencer graves
Brahm, David wrote:

            

  
    
#
On Fri, 2 Sep 2005, Brahm, David wrote:

            
Well, yes and no.

It is the result of a bug fix a version or two ago that dealt with the 
case where there was a local variable with the same name as a 
variable being modified by a complex superassignment.

As the R language definition now explains
   x[3] <<- 9
is short for
   `*tmp*` <- get(x, envir=parent.env, inherits=TRUE)
   `*tmp*`[3] <- 9
   x <<- `*tmp*`

and so it doesn't work if x doesn't exist in the parent environment.

x <<- 9
is ok, since it doesn't have to look up x before assigning it, but it is 
still a wart that x<<-9 creates a local variable x when executed in the 
global environment but not when executed anywhere else.

 	-thomas
#
On Fri, 2 Sep 2005, Spencer Graves wrote:

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

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.

 	-thomas
#
Wow!  That's great.  Thanks.  spencer
Thomas Lumley wrote: