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

From: Berton Gunter <>
Date: Tue 21 Jun 2005 - 09:29:53 EST


Thanks to Robert and Gabor for their replies, but neither was what I was looking for, undoubtedly because of the poor phrasing of my question (+ a typo -- however, even if correctly typed it doesn't work). I finally realized that the "elegant" approach I sought can easily be done without setGeneric:



Indeed, this is a general template for this sort of thing. Should have thought of this before, as V&R's S PROGRAMMING has numerous such examples... Sigh...

-----Original Message-----
From: Gabor Grothendieck [] Sent: Monday, June 20, 2005 1:31 PM
To: Berton Gunter
Subject: Re: [R] How to define S4 methods for '['

On 6/20/05, Berton Gunter <> 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'
> I would have thought that the following would work, but it doesn't:
> setMethod("[","foo",function(x,i, j, .,drop=TRUE)callGeneric(x@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
> 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),
> will suffice.

Download the source to the 'its' package where an S4 [ method is defined. mailing list PLEASE do read the posting guide! Received on Tue Jun 21 09:34:35 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:54 EST