Re: [Rd] "[" operator and indexing ambiguity

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Fri 26 Jan 2007 - 17:50:31 GMT

Hackish (and maybe expensive; does match.call duplicate the call arguments?), but maybe

setClass("A", "numeric")

setMethod("[",

          c(x="A"),
          function(x, i, j, ..., drop=TRUE) {
              "..." %in% names(match.call(expand.dots=FALSE))
          })

> a <- new("A")
> a[i]

[1] FALSE
> a[i,,]

[1] TRUE ?

Martin

"Bradley Buchsbaum" <bbuchsbaum@berkeley.edu> writes:

> Hi,
>
> I am working on writing some S4 classes that represent
> multidimensional (brain) image data. I would like these classes to
> support standard array indexing. I have been studying the Matrix and
> EBImage (http://www.bioconductor.org/packages/1.9/bioc/html/EBImage.html)
> packages to see how this is done.
>
> When using objects of the "array" class directly, R distinguishes
> between the calls:
>
> x[i,,] and x[i]
>
> with the former returning a 2D array of values and the latter
> returning a single value. The question I have is whether this same
> behavior can be simulated in classes that do not inherit from the
> "array" class directly? (See below for a snippet from the EBImage
> package which suggests that it cannot).
>
> My guess is that native array indexing is making use of the
> information provided by the commas, and this is unavailable to user
> implemented class methods?
>
> thanks,
>
> Brad Buchsbaum
>
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> setMethod("[", signature(x = "Image", i = "numeric", j = "missing"),
> function(x, i, j, k, ..., drop) {
> if (missing(k)) {
> warning("using index [int], cannot distinguish from
> [int,,ANY], use [int,1:dim(x)[2],ANY] otherwise")
> tmp = x@.Data[i, drop = FALSE]
> }
> else {
> tmp = x@.Data[i, , k, drop = FALSE]
> }
> ...
> }
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin Morgan
Bioconductor / Computational Biology
http://bioconductor.org

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat Jan 27 04:53:34 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 26 Jan 2007 - 19:32:53 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.