[Rd] Extract callNextMethod "array" calls "matrix"?
John Chambers
jmc at r-project.org
Wed Jul 21 17:58:14 CEST 2010
There does indeed seem to be a bug in the C code that implements
callNextMethod, with the effect of adding a spurious index to calls to
the primitive `[` code with more than 2 subscripts.
The message "incorrect number of dimensions" is telling the truth, the
primitive code gets 4 subscripts instead of 3 (note the "x[i = i, j = j,
NULL, ...]" in the error message).
Given the time of year, meetings, and the obscurity of this piece of
code, the bug won't likely be fixed soon, so any workaround that avoids
the use of callNextMethod on `[` with 3 or more subscripts is a good idea.
On 7/20/10 1:41 PM, Daniel Murphy wrote:
> I have a class that extends array and my method for "[" stops with an error:
>
>> setClass("A", contains="array")
> [1] "A"
>> setMethod("[", "A", function(x, i, j, ..., drop = TRUE) new("A",
> callNextMethod()))
> [1] "["
>> a<-new("A",array(1:12,c(4,3,1)))
>> a
> An object of class "A"
> , , 1
>
> [,1] [,2] [,3]
> [1,] 1 5 9
> [2,] 2 6 10
> [3,] 3 7 11
> [4,] 4 8 12
>
>> a[1:2,2:3,1]
> Error in x[i = i, j = j, NULL, ...] : incorrect number of dimensions
> Error in callNextMethod() : error in evaluating a 'primitive' next method
>>
>
> A similar error does not occur when extending a matrix:
>> setClass("M", contains="matrix")
> [1] "M"
>> setMethod("[", "M", function(x, i, j, ..., drop = TRUE) new("M",
> callNextMethod()))
> [1] "["
>> a<-new("M",matrix(1:12,4,3))
>> a[1:2,2:3]
> An object of class "M"
> [,1] [,2]
> [1,] 5 9
> [2,] 6 10
>>
>
> Is there a problem with my method definition for the array-extending class?
>
> My work-around is as follows:
>> setMethod("[", "A", function(x, i, j, ..., drop = TRUE) new("A",
> `[`(as(x,"array"), i=i, j=j, ..., drop=drop)))
> [1] "["
>> a[1:2,2:3,1]
> An object of class "A"
> [,1] [,2]
> [1,] 5 9
> [2,] 6 10
>>
>
> Cheers,
> Dan
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list