[R] RE: How to write an S4 method for sum or a Summary generic

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Apr 21 12:53:52 CEST 2004


As Doug pointed out, the problem is the generic you are trying to create.
I don't see how to add to his reply: for an S4 generic to be useful, you 
need named arguments to dispatch on.

Note than in S4 max and sum have arglist

function (x, ..., na.rm = FALSE)

and that is different from S3 and R.  If you start with your own generic 
with that arglist, you should be able to add S4 methods.

On Wed, 21 Apr 2004, Swinton, Jonathan wrote:

> I asked last week if anyone knew how to define an S4 method for "sum" or any
> member of the "Summary" group generics. Doug Bates pointed out that
> 
> 	>There is no x argument in the generic so you can't dispatch on it.
> 
> 	> sum
> 	function (..., na.rm = FALSE) 
> 	.Internal(sum(..., na.rm = na.rm))
> 
> but I don't know if he meant to imply that therefore there was no way of
> doing this. 

That is what he said, as I read it.

> I had no other replies. I have now grepped all the R sources,
> and all the  CRAN or Bioconductor packages, and found no example of any
> setMethod("sum or setMethod("Summary. (Moreover it seems to me  that the
> implementation of setGeneric("max" proposed on p351 of the Green Book, and
> designed explicitly to allow S4 methods to be written for "Summary" group
> generics, is not in fact implemented in R, but I don't understand the code
> well enough.)
> 
> Is it the opinion of r-help or the R core that it is not possible to do
> this? To be explicit, given
> 	
> >setGeneric("sumLike",function(...,na.rm=FALSE)standardGeneric("sumLike"))
> 	>setClass("Foo",representation(a="numeric"));
> 	>aFoo <- new("Foo",a=c(1,NA))
> 	>.sum.Foo <- function(x,na.rm) {sum(x at a,na.rm)}
> 
> Then is there a setMethod call for sumLike which will despatch
> sumLike(aFoo,na.rm) to  .sum.Foo ? If not, might this be considered a bug?

Not a bug in R.  With no argument to dispatch on, how can dispatch be 
done?

> 
> 
> 
> 
> >  -----Original Message-----
> > From: 	Swinton, Jonathan  
> > Sent:	19 April 2004 13:16
> > To:	'r-help at stat.math.ethz.ch'
> > Subject:	How to write an S4 method for sum or a Summary generic
> > 
> > If I have a class Foo, then i can write an S3 method for sum for it:
> > 
> > >setClass("Foo",representation(a="integer"));aFoo=new("Foo",a=
> > c(1:3,NA))
> > >sum.Foo <- 
> > function(x,na.rm){print(x);print(na.rm);sum(x at a,na.rm=na.rm)}
> > >sum(aFoo)
> > 
> > But how do I write an S4 method for this? All my attempts to 
> > do so have foundered. For example
> > >setMethod("sum",signature("Foo","logical"), 
> > function(x,na.rm){print(x);print(na.rm);sum(x at a,na.rm=na.rm)}
> > creates a method which seems to despatch on na.rm="Foo":
> > > getMethods("sum")
> > na.rm = "ANY":
> > function (..., na.rm = FALSE) 
> > .Internal(sum(..., na.rm = na.rm))
> > 
> > na.rm = "Foo":
> > function (..., na.rm = FALSE) 
> > {
> >     .local <- function (x, na.rm) 
> >     {
> >         print(x)
> >         print(na.rm)
> >         sum(x at a, na.rm = na.rm)
> >     }
> >     .local(..., na.rm = na.rm)
> > }
> > 
> > na.rm = "missing":
> > function (..., na.rm = FALSE) 
> > .Internal(sum(..., na.rm = na.rm))
> > ##:    (inherited from na.rm = "ANY")
> > 
> > Pages 350-352 of the Green book discuss at some length how to 
> > write a generic function for Summary group generics which 
> > uses tail recursion to allow the correct method to be called 
> > on each member of a ... argument list.  But it gives no 
> > examples of what individual method functions need to look 
> > like. Any ideas or a place to look for working code?
> > 
> > Jonathan Swinton, Statistical Scientist, Computational 
> > Biology, Pathway Analysis, Global Sciences and Information, 
> > AstraZeneca.
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list