title: Overloading [ (subset) function with 3 dimensions in S4 class
body: <p>I have an S4 class (let's call it <code>myclass</code>) that I'd like to overload the subsetting operation for.  Objects in the class can be 3 or more dimensional, and I'd like to have the extraction operator <code>[</code> work the same way it works for arrays.  Specifically, I'd like to be able to enable use of missing indices, like when subsetting R arrays (e.g. <code>myarray[2, ,1:3]</code> for a 3D array).  </p>
  
  <p>For 2D objects, this is not a problem.  To enable, e.g., <code>object[, 1:3]</code> I simply use <code>setMethod</code> as follows: </p>
  
  <pre><code>setMethod(f = "[", signature(x = "myclass", i = "missing", j = "numeric"),
    definition = function(x, i, j, k = NA, l = NA, ..., drop) {
    i &lt;- 1:(dim(x)[1])
    return(myfun(x, i, j, k, l, ..., drop))  
  })
  </code></pre>
  
  <p>Because the <code>[</code> function only has two named required arguments (<code>i</code> and <code>j</code>), I can't figure out a way to extend this to 3D or 4D objects.  </p>
  
  <p>I would like to create a generic <code>[</code> function, but R doesn't let me create a generic from the <code>[</code> operator: <code>setGeneric('[', signature=c('i', 'j', 'k'))</code> errors with <code>‘[’ is a primitive function;  methods can be defined, but the generic function is implicit, and cannot be changed.</code></p>
  
  <p>In John Chambers' <a href="https://developer.r-project.org/howMethodsWork.pdf" rel="nofollow">article</a> on S4 classes, he seems to imply that the Matrix package does this, but I couldn't tell how they did it and looking through the Matrix source code didn't show me any obvious examples.</p>