[R] RE: How to write an S4 method for sum or a Summary generic
Swinton, Jonathan
Jonathan.Swinton at astrazeneca.com
Fri May 14 18:03:01 CEST 2004
I asked a couple of weeks ago about how to write an S4 method for sum, or
another member of the Summary group generics. This prompted an informed and
somewhat over my head email discussion off list, but for the benefit of
posterity the current answer is
1) Redefine the generic for sum as follows:
-----------
setGeneric("sum", function(x, ..., na.rm = FALSE)
standardGeneric("sum"),group="Summary",
useAsDefault = function(x, ..., na.rm = FALSE) .Internal(sum(x, ...,na.rm
= na.rm)))
-----------
2) This allows the following to work as expected
----------
setClass("Foo", representation(a="numeric"))
aFoo <- new("Foo", a=c(1,NA))
setMethod("Summary", "Foo",
function(x, ..., na.rm = FALSE) {
cat("Foo method\n")
callGeneric(x at a, ..., na.rm = na.rm)
})
sum(aFoo)
sum(aFoo, na.rm=TRUE)
----------
Without step 1) this does not work in R 1.9.0 but is reported to work in
S-Plus 6.2.
3) Repeat for each other Summary group generic you wish to write a method
for.
Thanks to Brian Ripley and John Chambers for taking the time on this.
Jonathan Swinton, Statistical Scientist, Computational Biology, Pathways,
Global Sciences and Information, AstraZeneca, Alderley Park, Cheshire SK10
4TG UK
More information about the R-help
mailing list