[Rd] Strange behaviour of the [[ operator

Duncan Murdoch murdoch at stats.uwo.ca
Sat Sep 30 21:16:10 CEST 2006


On 9/30/2006 10:39 AM, Martin Maechler wrote:
>>>>>> "Herve" == Herve Pages <hpages at fhcrc.org>
>>>>>>     on Fri, 29 Sep 2006 18:06:41 -0700 writes:
> 
>     Herve> Hi,
>     Herve> This looks like a bug:
> hmm, maybe...
> or maybe not; I vaguely recall that this is documented somewhere...
> 
>     >> a <- list(b=5)
>     >> a[['b']]
>     Herve> [1] 5
>     >> a[[t<-'b']]
> 
>     Herve> Nothing gets printed!
> 
> Yes, but that is not really much related to "[[" 
> but rather to  "<-"  which momentarily turns off auto-printing.

I think this is probably not intentional, because the behaviour is so 
inconsistent.  In names.c, the "eval" entry for "[" and "[[" is 0, which 
is supposed to indicate R_Visible is on.  It's the same as for c(), 
substitute(), quote(), interactive(), expression(), nargs(), and those 
return a visible result.

On the other hand, dim(), dimnames(), as.character(), call(), rep(), 
seq.int(), seq_along() also have eval=0, and they give an invisible result.

Among the eval=10 functions, I see visible results from unlist(), 
cbind(), rbind(), and invisible ones from sys.parent(), sys.call() etc., 
  parent.frame(), and so on (I gave up testing at this point).

I suspect the problem is that these are low level functions where the 
evaluator does almost nothing automatically, and handling R_Visible was 
just overlooked.

Duncan Murdoch



> 
> Similar things happen in many similar circumstances.
> 
>     Herve> I need to use parenthesis to see the expected result:
> 
>     >> a[[(t<-'b')]]
>     Herve> [1] 5
> 
>     Herve> Cheers,
>     Herve> H.
> 
> Martin
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel




More information about the R-devel mailing list