[Rd] setMethod("Summary")

Martin Maechler maechler at stat.math.ethz.ch
Tue Sep 5 10:35:12 CEST 2006


>>>>> "Robin" == Robin Hankin <r.hankin at noc.soton.ac.uk>
>>>>>     on Tue, 5 Sep 2006 08:58:23 +0100 writes:

    Robin> Dear Franklin
    Robin> thank you for this.

yes, and he *did* send it already last week.

    Robin> Your suggestion works.

and continues to do so in R-devel aka "R-2.4.0 alpha" (as of today?)

    Robin> But now I'm confused because I'm not sure how  
    Robin> the
    Robin> setGeneric() call actually helps.

Robin, I think you got confused because you are mixing S3 and S4
("max.brob" is S3).

You should (typically) not define S3 methods for S4 classes,
and you should definitely not do it in this case where Franklin 
has shown the correct S4 way.
(and for my taste you should also not do it for the "Logic" case
 in that other thread last week: It does not bring you anything and is
 ugly for me, from the point of view of S4.)

BTW: As Franklin mentioned last time, he has most probably
learned the example from the example in the 'Matrix' package.
Since we develop the Matrix package using a world-readable
subversion archive, you can always have a look there.
The  "Summary" [ "max" etc ] generic definitions are in
   https://svn.R-project.org/R-packages/trunk/Matrix/R/AllGeneric.R
BTW, the above file contains
>> ## "Summary" --- this needs some hoop jumping that may become unnecessary
>> ##               in a future version of R (>= 2.3.x):

where the hope there has not yet been fulfilled ...

Martin Maechler, ETH Zurich


    Robin> If I source:

    Robin> setClass("brob",
    Robin> representation = representation 
    Robin> (x="numeric",positive="logical"),
    Robin> prototype      = list(x=numeric(),positive=logical())
    Robin> )


    Robin> max.brob <- function(..., na.rm=FALSE){stop("not YET implemented")}


    Robin> ---ie no call to setGeneric()--- then in an R session I can do the  
    Robin> following:


    R> x <- new("brob",x=6,positive=T)
    R> max(x)
    Robin> Error in max.brob(..., na.rm = na.rm) : not YET implemented


    Robin> So max(x) calls max.brob() as intended.  Why would a call to  
    Robin> setGeneric()
    Robin> be desirable here?  What do I gain from it?


    Robin> thanks again


    Robin> Robin




    Robin> On 4 Sep 2006, at 19:58, Parlamis Franklin wrote:

    >> i believe, if the function you are trying to work with has "..." as
    >> the first formal argument (as do most if not all of the "Summary"
    >> group functions), you will need to redefine the generic in order to
    >> provide a "real" S4 argument on which to dispatch.
    >> 
    >> the following works for me (IIRC it was martin who initially pointed
    >> me in this direction, so any thanks are his).
    >> 
    >> setGeneric("max", function(x, ..., na.rm = FALSE)
    >> {
    >> standardGeneric("max")
    >> },
    >> useAsDefault = function(x, ..., na.rm = FALSE)
    >> {
    >> base::max(x, ..., na.rm = na.rm)
    >> },
    >> group = "Summary")
    >> 
    >> i don't have bleeding edge devel version installed, so i am not sure
    >> whether recent changes to S4 have mooted the above.
    >> 
    >> (also, i thought i sent this last week, but i may have neglected to
    >> make it plain text, so maybe you didn't get it)
    >> 
    >> franklin parlamis
    >> 
    >> On Sep 4, 2006, at 3:47 AM, Robin Hankin wrote:
    >> 
    >>> Hi everyone and thanks for being patient.   I've used "!.foo"() et
    >>> seq pro tem.
    >>> 
    >>> Next problem: how to define "Summary" methods for brobs.
    >>> 
    >>> ?max says
    >>> 
    >>> 'max' and 'min' are generic functions: methods can be defined for
    >>> them individually or via the 'Summary' group generic. For
    >>> this to
    >>> work properly, the arguments '...' should be unnamed.
    >>> 
    >>> OK, so what is the correct procedure to make sum() operate on brob
    >>> objects?
    >>> 
    >>> My best effort follows.
    >>> 
    Robin> [snipped]


    Robin> --
    Robin> Robin Hankin
    Robin> Uncertainty Analyst
    Robin> National Oceanography Centre, Southampton
    Robin> European Way, Southampton SO14 3ZH, UK
    Robin> tel  023-8059-7743

    Robin> ______________________________________________
    Robin> R-devel at r-project.org mailing list
    Robin> https://stat.ethz.ch/mailman/listinfo/r-devel




More information about the R-devel mailing list