[R] Question about <<- assignment

Rolf Turner r.turner at auckland.ac.nz
Thu Jul 2 03:34:34 CEST 2009


On 2/07/2009, at 12:20 PM, Hsiu-Khuern Tang wrote:

> Is this expected behavior?
>
>> z <- 1:5
>> z[1] <<- 0
> Error in z[1] <<- 0 : object "z" not found
>
> The documentation seems to suggest that z will be found in the global
> environment and modified accordingly.

I would agree that the documentation would *suggest* that, but it
doesn't explicitly say it, and no more it should, since the "[<<-"
operator does not exist.  Unlike the "[<-" operator which does exist.

Moreover there is little reason for a "[<<-" operator to exist.

The basic raison-d'etre for the "<<-" operator is to allow one to
assign objects in the global environment from within a function.
But you shouldn't do it, even though it is possible!!!

Modifying a component of an existing object in the global environment
from within a function is even more to be avoided, it seems to me.  And
the designers of R seem to have drawn the line at this point.

There may be conceptual difficulties with implementing a "[<<-"  
operator,
over and above the philosophical position that using such an operator
would be bad practice.  I don't know.  But I doubt that R Core will move
to build a "[<<-" operator, even should it prove to be possible.

The error message ``object "z" not found'' is certainly misleading.   
I don't
know how hard it would be to change the code so that a more enlightening
error message gets issued.  Probably not worth the effort since few  
users
would ever entertain the idea of using "[<<-".

	cheers,

		Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list