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

Swinton, Jonathan Jonathan.Swinton at astrazeneca.com
Wed Apr 21 12:10:38 CEST 2004


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. 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?




>  -----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.
>




More information about the R-help mailing list