[Rd] surprising behaviour of names<-
Berwin A Turlach
berwin at maths.uwa.edu.au
Mon Mar 16 03:26:25 CET 2009
On Sun, 15 Mar 2009 21:01:33 +0100
Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
> Berwin A Turlach wrote:
> > Obviously, assuming that R really executes
> > *tmp* <- x
> > x <- "names<-"('*tmp*', value=c("a","b"))
> > under the hood, in the C code, then *tmp* does not end up in the
> > symbol table and does not persist beyond the execution of
> > names(x) <- c("a","b")
> to prove that i take you seriously, i have peeked into the code, and
> found that indeed there is a temporary binding for *tmp* made behind
> the scenes -- sort of. unfortunately, it is not done carefully enough
> to avoid possible interference with the user's code:
> '*tmp*' = 0
> # 0
> x = 1
> names(x) = 'foo'
> # error: object "*tmp*" not found
I agree, and I am a bit flabbergasted. I had not expected that
something like this would happen and I am indeed not aware of anything
in the documentation that warns about this; but others may prove me
wrong on this.
> given that `*tmp*`is a perfectly legal (though some would say
> 'non-standard') name, it would be good if somewhere here a warning
> were issued -- perhaps where i assign to `*tmp*`, because `*tmp*` is
> not just any non-standard name, but one that is 'obviously' used
> under the hood to perform black magic.
Now I wonder whether there are any other objects (with non-standard)
names) that can be nuked by operations performed under the hood.
I guess the best thing is to stay away from non-standard names, if only
to save the typing of back-ticks. :)
Thanks for letting me know, I have learned something new today.
More information about the R-devel