On 01/07/2009 9:34 PM, Rolf Turner wrote:
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!!!

No, I'd say that's an unfortunate compromise. The real use is for nested evaluation: a little worker function defined within a bigger function needs to access the parent's variables for reading (no problem, lexical scope gives that) and writing (that's what <<- is for).

So what to do when the target of <<- doesn't exist? I'd say a better solution than we have is for it to fail, rather than assign in the global environment.

Duncan Murdoch


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}}

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to