[R] How to define S4 methods for '['

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 20 22:31:12 CEST 2005


On 6/20/05, Berton Gunter <gunter.berton at gene.com> wrote:
> Folks:
> 
> This is a question about the S4 formal class system.
> 
> Suppose I have a class, 'foo', defined by:
> 
> setClass('foo',representation(dat='matrix', id='character') )
> 
> I wish to define a '[' method for foo that will extract from the 'dat' slot.
> I would have thought that the following would work, but it doesn't:
> 
> setMethod("[","foo",function(x,i, j, .,drop=TRUE)callGeneric(x at dat,i,
> j,drop=drop) )
> 
> The only way I have succeeded in defining this method is using brute force
> eval(parse(. :
> 
> {eval(parse(text=paste('.dat(x)[',
>        ifelse(missing(i),',','i,'),
>        ifelse(missing(j),']','j]'))))
>        }
> 
> This works. However, I am not able under any circumstances to pass the drop
> argument -- it is ignored.
> 
> I would appreciate any pointers about how to do this properly. If  this is
> explicitly in the Green Book (I do not have it with me at the moment), that
> will suffice.
> 

Download the source to the 'its' package where an S4 [ method is defined.




More information about the R-help mailing list