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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to