If you plan to do this for certain (non-primitive) "Math" and "Math2"  
group functions you will also need setGeneric calls of this form:

setGeneric("log", group="Math")

This much is documented in the "group generic" doc page.  What is  
undocumented (AFAIK), and tripped me up for a while before Martin, I  
believe, handed me the answer, is that, 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  
jump through an initial hoop in order to provide the methods package  
with a "real" S4 argument on which to dispatch.  For example,

setGeneric("max", function(x, ..., na.rm = FALSE)
        {
                standardGeneric("max")
        },
        useAsDefault = function(x, ..., na.rm = FALSE)
        {
                base::max(x, ..., na.rm = na.rm)
        },
        group = "Summary")

Franklin Parlamis

On Aug 25, 2006, at 3:25 AM, Martin Maechler wrote:

>>>>>> "BDR" == Prof Brian Ripley <[EMAIL PROTECTED]>
>>>>>>     on Fri, 25 Aug 2006 12:56:17 +0100 (BST) writes:
>
>     BDR> Just like any other S4 method: setMethod("+",
>     BDR> c("track", "track"), function(e1, e2) new("track",
>     BDR> x=c([EMAIL PROTECTED], [EMAIL PROTECTED]), y=c([EMAIL 
> PROTECTED],[EMAIL PROTECTED])))
>
>     BDR> If you want to write a group generic for the S4 Ops
>     BDR> group, you do it very like S3.  There are worked
>     BDR> examples in 'S Programming' (that at least at one point
>     BDR> worked in R).
>
> Indeed.
> The 'Matrix' package has examples for both:
> Most of the time, we define group generics for "Arith",
> but we have one method for "+" (for "dgTMatrix" operands, where
> "+" can be done in particular way).
>
> Inside a source version of Matrix, use
>     grep 'setMethod("Arith"'  R/*.R
> or  grep 'setMethod("+"'  R/*.R
> to find them.
> Also look at NAMESPACE and R/AllGeneric.R
>
> Martin Maechler, ETH Zurich
>
>     BDR> On Fri, 25 Aug 2006, Robin Hankin wrote:
>
>>> Hi
>>>
>>> I'm trying to implement S4 methods in a package, and I am
>>> having difficulty defining "+" to do what I want.
>>>
>>> In the Green Book, there is a discussion of a "track"
>>> object,
>>>
>>> setClass("track", representation(x="numeric",
>>> y="numeric"))
>>>
>>> OK.
>>>
>>> track1 <- new("track",x=c(1,4,6),y=c(10,11,12)) track2 <-
>>> new("track",x=c(2,5),y=c(100,101))
>>>
>>>
>>> What I want to do is to define "+" for track object so
>>> that if
>>>
>>> track3 <- track1 + track2
>>>
>>> has [EMAIL PROTECTED] == c(1,2,4,5,6) and [EMAIL PROTECTED] =
>>> c(10,100,11,101,12)
>>>
>>> maybe adding a track object to a scalar would shift the
>>> values of the x slot.
>>>
>>> The algorithm itself is no problem...but what is the S4
>>> equivalent to the S3 technique of writing an Ops.track()
>>> function that tells R what "+" means?
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Robin Hankin Uncertainty Analyst National Oceanography
>>> Centre, Southampton European Way, Southampton SO14 3ZH,
>>> UK tel 023-8059-7743
>>>
>>> ______________________________________________
>>> R-devel@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>
>     BDR> -- Brian D. Ripley, [EMAIL PROTECTED] Professor of
>     BDR> Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>     BDR> University of Oxford, Tel: +44 1865 272861 (self) 1
>     BDR> South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG,
>     BDR> UK Fax: +44 1865 272595
>
>     BDR> ______________________________________________
>     BDR> R-devel@r-project.org mailing list
>     BDR> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


        [[alternative HTML version deleted]]

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

Reply via email to