[R] trouble with S4 methods for group "Summary"

Parlamis Franklin fparlamis at mac.com
Thu Dec 29 06:52:00 CET 2005

Hello.  This question concerns the Methods package.  I have created a  
new class and am trying to set a method for it for S4 group generic  
"Summary".  I have run into some signature problems.  An example:

	> setClass("track", representation(x="numeric", y="character"))
	[1] "track"
	> setGeneric("max", group="Summary")
	[1] "max"
	> setMethod("Summary", signature(x="track"), function(x, ..., na.rm)  
callGeneric(x at x, ..., na.rm))
	[1] "Summary"
	> dd<-new("track", x=c(1,2), y="abc")
	> max(dd)
	[1] -Inf
	Warning message:
	no finite arguments to max; returning -Inf
	> showMethods("max")
	Function "max":
	na.rm = "ANY"
	na.rm = "track"
	na.rm = "missing"
	    (inherited from na.rm = "ANY")

As you can see from the above, the method I tried to set for  
"max" (via "Summary") was defined for the formal argument "na.rm" not  
"x".  This makes sense because the standardGeneric created for max  
only allows methods to be defined for argument "na.rm"

	> max
	standardGeneric for "max" defined from package "base"
   	belonging to group(s): Summary
	function (..., na.rm = FALSE)
	<environment: 0x19447a28>
	Methods may be defined for arguments: na.rm

However, group "Summary" purports to allow you to define methods for  
arguments "x" and "na.rm".

	> Summary
	groupGenericFunction for "Summary" defined from package "base"
	function (x, ..., na.rm = FALSE)
	stop("function 'Summary' is a group generic; do not call it directly",
	    domain = NA)
	<environment: 0x16aef098>
	Methods may be defined for arguments: x, na.rm

How does this work?  Can someone point me to where I am going wrong,  
and explain how to define S4 methods for group "Summary" for argument  
"x"?  Perhaps I need to do more in my "setGeneric" call?  Thanks in  

More information about the R-help mailing list