title: S4 class is not subsettable
body: <p>I am trying to write a subsetting method for an S4 class. I get <code>this S4 class is not subsettable</code> error whatever I tried.</p>
  
  <p>Here is a minimal example:</p>
  
  <pre><code>setClass(Class = "A", representation = representation(ID = "character"))
  setClass(Class = "B", representation = representation(IDnos = "list"))
  
  a1 &lt;- new(Class = "A", ID = "id1")
  a2 &lt;- new(Class = "A", ID = "id2")
  
  B1 &lt;- new(Class = "B", IDnos = c(a1, a2))
  </code></pre>
  
  <p>When I type: </p>
  
  <pre><code>B1@IDnos[[1]]
  </code></pre>
  
  <p>I get what I want:</p>
  
  <blockquote>
  <pre><code>&gt; An object of class "A"
  &gt; Slot "ID":
  &gt; [1] "id1"
  </code></pre>
  </blockquote>
  
  <p>But I want to get this by just writing something like: <code>B1[1]</code> or if not by <code>B1[[1]]</code> </p>
  
  <p>From <a href="https://stackoverflow.com/questions/10961842/how-to-define-the-subset-operators-for-a-s4-class/10961998#10961998">THIS</a> post, I got some idea and tried to mimic what author wrote. But it didn't work in my case:</p>
  
  <pre><code>setMethod("[", c("B", "integer", "missing", "ANY"),
            function(x, i, j, ..., drop=TRUE)
            {
              x@IDnos[[i]]
              # initialize(x, IDnos=x@IDnos[[i]])    # This did not work either
            })
  B1[1]
  
  &gt; Error in B1[1] : object of type 'S4' is not subsettable
  </code></pre>
  
  <p>Following code did not work either: </p>
  
  <pre><code>setMethod("[[", c("B", "integer", "missing"),
            function(x, i, j, ...)
            {
              x@IDnos[[i]]
            })
  B1[[1]]
  
  &gt; Error in B1[[1]] : this S4 class is not subsettable
  </code></pre>
  
  <p>Any ideas?</p>