[Rd] S4 methods for "+"

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 25 13:56:17 CEST 2006


Just like any other S4 method:

setMethod("+", c("track", "track"), 
          function(e1, e2) new("track", x=c(e1 at x, e2 at x), y=c(e1 at y,e2 at y)))

If you want to write a group generic for the S4 Ops group, you do it
very like S3.  There are worked examples in 'S Programming' (that at least 
at one point worked in R).


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 track3 at x  == c(1,2,4,5,6)
> and
> track3 at y = 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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list