From sfalcon at fhcrc.org  Sat Sep 30 18:38:31 2006
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Sat, 30 Sep 2006 09:38:31 -0700
Subject: [Rd] Strange behaviour of the [[ operator
In-Reply-To: <17694.33160.489389.196778@stat.math.ethz.ch> (Martin Maechler's
	message of "Sat, 30 Sep 2006 16:39:04 +0200")
References: <451DC321.2030704@fhcrc.org>
	<17694.33160.489389.196778@stat.math.ethz.ch>
Message-ID: <m2irj5e2d4.fsf@ziti.local>

Martin Maechler <maechler at stat.math.ethz.ch> writes:
>     >> 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.

Yes, not being related to "[[" is exactly why it is surprising.

I would expect vv[[f(x)]] to print regardless of f's auto-printing
behavior.

f1 <- function(x) x
f2 <- function(x) return(invisible(x))
a <- list(b=5)
a[[f1("a")]]
a[[f2("a")]]

> Similar things happen in many similar circumstances.

Here's a similar thing:

> v <- 1:5
> v
[1] 1 2 3 4 5
> v[mustBeDocumentedSomewhere=3]
[1] 3

And this can be confusing if one thinks that subsetting is really a
function and behaves like other R functions w.r.t. to treatment of
named arguments:

> m <- matrix(1:4, nrow=2)
> m
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> m[j=2]
[1] 2




[Rd] Strange behaviour of the [[ operator
Seth Falcon