[Bioc-devel] Overloading subset operator for an S4 object with more than two dimensions

Christian Arnold christian.arnold at embl.de
Thu May 14 12:35:36 CEST 2015


Hi there,

I am about to develop a Bioconductor package that implements a custom S4 
object, and I am currently thinking about a few issues, including the 
following:

Say we have an S4 object that stores a lot of information in different 
slots. Assume that it does make sense to extract information out of this 
object in four different "dimensions" (conceptually similar to a 
four-dimensional object), so one would like to use the subset "[" 
operator for this, but extending beyond the "typical" one or two 
dimensions to 4:

setClass("A", 
representation=representation(a="numeric",b="numeric",c="numeric",d="numeric"))
a = new("A", a=1:5,b=1:5,c=1:5,d=1:5)

Now it would be nice to do stuff like a[1,2,3:4,5], which should simply 
return the selected elements in slots a, b, c, and d, respectively. So 
a[1,2,3:4,5] would return:

An object of class "A"
Slot "a":
[1] 1

Slot "b":
[1] 2

Slot "c":
[1] 3 4

Slot "d":
[1] 5

This is how far I've come:

setMethod("[", c("A", "ANY", "ANY","ANY"),
           function(x, i, j, ..., drop=TRUE)
           {
             dots <- list(...)
             if (length(dots) > 2) {
               stop("Too many arguments, must be four dimensional")
             }

             # Parse the extra two dimensions that we need from the ... 
argument
             k = ifelse(length(dots) > 0 , dots[[1]], c(1:5))
             l = ifelse(length(dots) == 2, dots[[2]], c(1:5))

             initialize(x, a=x at a[i],b=x at b[j],c=x at c[k],d=x at d[l])
           })

This works for stuff like a[1,2,3, 4], but fails with a general error if 
one of the indices is a vector such as a[1:2,2,3, 4] or a[1,2,3,4:5].


So, in summary, my questions are:
1. Is there a reasonable way of achieving the 4-dimensional subsetting 
that works as a user would expect it to work?
2. Does it make more sense to write a custom function instead to achieve 
this, such as subsetObject() without overloading "[" explicitly? What 
are the Bioconductor recommendations here?

I'd appreciate any help, suggestions, etc!

Thanks,
Christian



More information about the Bioc-devel mailing list