[R] an extension to 'array'

Heywood, Giles Giles.Heywood at CommerzbankIB.com
Tue Mar 2 18:54:09 CET 2004


1. I want to extend the 'array' class, and prefer to use S4 in the belief
that this is the best structure for new projects (as the documentation
says).  I actually wish to do something similar to the excellent Oarray by
Jonathan Rougier, but as this class is S3, I can't see how to extend it by
S4.  Am I being dumb here?

2. Seeking then to define a similar class in S4, my principal task is to
modify (extend?) a the extractor "[", modifying the range variables i,j,...
. I run into problems here, though.  The following is a minimal example:

setClass("xarray",representation("array"))

setMethod(f="[", 
       signature=signature("xarray","ANY"),  
       definition=function(x,i,j, ..., drop)
                    {
                    xcall <- match.call()
			  #sundry processes on xcall to adjust i,j,...
                    y <- x at .Data
                    xcall[[2]] <- as.name("y")
                    eval(xcall)
                    }
       )       

foo <- new("xarray",array(1:24,c(2,3,4)))
foo[1,1,1,drop=FALSE]	#gives expected result i.e. a 1 in a [1,1,1] array
foo[1,1,,drop=FALSE]	#again gives the expected result in a [1,1,4] array
foo[1,,,drop=FALSE]	#gives an error "Error in y[i = 1, , drop = FALSE] :
incorrect number of dimensions"

Examining match.call() e.g. in the browser shows that there is indeed one
fewer argument than is required 

Called from: foo[1, , , drop = FALSE]
Browse[1]> xcall
y[i = 1, , drop = FALSE]

3.  I welcome input on this, at different levels, including the following:

- Is there a relatively small correction to what I have attempted and all is
well? 

- Can I instead extend the (perfectly functional) S3 class Oarray using S4?


- What is the better way to do what I am attempting in the last 3 lines of
my method i.e. a small tweak to the arguments of a S4 class using variables
from its slots, then calling the method for the superclass?

- Is there a package on CRAN which, by my studying it closely, I will gain
enlightenment?

I might also get RTFM, but then I *have* read quite a lot of the
documentation.  If the advice is RTFM, I request some assistance in finding
the correct sections for my question.  I would happily read the whole of the
Green Book if I could gain enlightenment from it, but there have been a lot
of changes since it was written.

Thanks

- Giles


********************************************************************** 
This is a commercial communication from Commerzbank AG.\ \ T...{{dropped}}




More information about the R-help mailing list