[Rd] More strange [[ behaviour

Duncan Murdoch murdoch at stats.uwo.ca
Mon Oct 2 21:45:23 CEST 2006


On 10/2/2006 3:21 PM, Herve Pages wrote:
> Hi,
> 
>> setClass("MyList", "list")
> [1] "MyList"
>> a <- new("MyList")
>> a
> An object of class "MyList"
> list()
>> setMethod("[[", "MyList", function(x, i, j, ...) cat("Just testing\n"))
>> a[[]]
> Just testing
>> a[[1]]
> Just testing
>> a[[a=1]]
> Just testing
>> a[[b=1]]
> Just testing
> ...
>> a[[v=1]]
> Just testing
>> a[[w=1]]
> Just testing
>> a[[x=1]]
> Error in a[[x = 1]] : subscript out of bounds
>> a[[y=1]]
> Just testing
>> a[[z=1]]
> Just testing
> 
> Can someone explain me why the "[[x=1]]" case is treated differently?

Indexing is a function call, with arguments x, i, j, ... .  If you use 
y=1, you're setting something in the "..." part of the arg list.  If you 
say x=1, you're setting the first arg, but because a[[x=1]] is the same 
as "[["(a, x=1), it is going to evaluate it as "[["(x=1, i=a) and try to 
index 1 by a instead of a by 1.

Duncan Murdoch




More information about the R-devel mailing list