title: Custom class inheriting `data.frame` and replacement method
body: <p>I defined a class (<code>tdtfile</code>), which inherits <code>data.frame</code>. I am now trying to define a <code>[.data.frame</code>-equivalent replacement method to return an appropriate object of class <code>tdtfile</code> rather than <code>data.frame</code>, but am having trouble.</p>
  
  <p>Here is What I'm doing:</p>
  
  <pre><code># Define Class
  setClass("tdtfile",
    representation(Comment = "character"),
     prototype(Comment = NULL),
     contains = c("data.frame"))
  
  # Construct instance and populate
  test &lt;- new("tdtfile",Comment="Blabla")
  df &lt;- data.frame(A=seq(26),B=LETTERS)
  for(sName in names(getSlots("data.frame"))){
    slot(test,sName) &lt;- slot(df,sName)
  }
  
  # "Normal" data.frame behavior (loss of slot "Comment")
  str(test[1])
  # Works as well - will be trying to use that below
  `[.data.frame`(test,1)
  
  # Try to change replacement method in order to preserve slot structure 
  # while accessing data.frame functionality
  setMethod(
    `[`,
    signature=signature(x="tdtfile"),
    function(x, ...){
      # Save the original
      storedtdt &lt;- x
      # Use the fact that x is a subclass to "data.frame"
      tmpDF &lt;- `[.data.frame`(x, ...)
      # Reintegrate the results
      if(inherits(x=tmpDF,what="data.frame")){
        for(sName in names(getSlots("data.frame"))){
          slot(storedtdt,sName) &lt;- slot(tmpDF,sName)
        }
        return(storedtdt)
      } else {
        return(tmpDF)
      }
    })
  
  # Method does not work - data.frame remains complete. WHY?
  str(test[1])
  
  # Cleanup
  #removeMethod(
  #  `[`,
  #  signature=signature(x="tdtfile"))
  </code></pre>
  
  <p>When calling something like</p>
  
  <pre><code>tdtfile[1]
  </code></pre>
  
  <p>this returns a a <code>tdtfile</code> object with all contained <code>data.frame</code> columns rather than just the first ... can anyone spot what I'm missing?</p>
  
  <p>Thank you for your help.</p>
  
  <p>Sincerely, Joh</p>