[Rd] Extract callNextMethod "array" calls "matrix"?

From: Daniel Murphy <chiefmurphy_at_gmail.com>
Date: Tue, 20 Jul 2010 13:41:36 -0700


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 Received on Tue 20 Jul 2010 - 20:44:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 21 Jul 2010 - 16:10:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive