[Rd] Overloading methods in R

Ali - saveez at hotmail.com
Fri Apr 22 15:10:35 CEST 2005


>From: "A.J. Rossini" <blindglobe at gmail.com>
>
>You are mixing S3 and S4 paradigms.   You want setMethod to define the
>method, not the  FunctionName.ClassOfObject S3 specification.

That just proves my newbie-ness in R programming. I would appreciate it if 
you could provide me the S4 equivalent. According to the documentation in 
the methods package:

"Computations using the notion of 'NULL' class attributes or
of class attributes with multiple class names are not really
compatible with the ideas in the 'methods' package."

So I guess a different approach should be taken in S4?


>On 4/22/05, Ali - <saveez at hotmail.com> wrote:
> >
> > >
> > >
> > >f <- function(...) UseMethod("f", NULL)
> > >
> > >f.NULL <- function(...) {
> > >       args <- list(...)
> > >       classes <- sapply(args, class)
> > >       .Class <- paste(classes, collapse = ".")
> > >       NextMethod("f", ...)
> > >}
> > >
> > >f.numeric <- function(...) 2 * ..1
> > >f.numeric.numeric <- function(...) ..1 + ..2
> > >f.character.numeric.Date <- function(...) {
> > >    args <- list(...)
> > >    paste(args[[1]], args[[2]], format(args[[3]], "%Y-%m-%d"))
> > >}
> > >f.default <- function(...) print(list(...))
> > >
> > >
> > >f(1)   # 2
> > >f(1,2) # 3
> > >f("a", 23, Sys.Date()) # "a 23 2005-04-21"
> > >f()    # list()
> >
> > Thanks Gabor! This answers a big part of my question. I am just curious 
>why
> > something like this doesn't work in S4:
> >
> > -------------------------
> > setGeneric("foo", function(object, ...) standardGeneric("foo"))
> >
> > foo.NULL <- function(object, ...) {
> >         args <- list(...)
> >         classes <- sapply(args, class)
> >         .Class <- paste(classes, collapse = ".")
> > }
> >
> > foo.default <- function(object, ...) paste("wrong args!")
> >
> > foo.numeric <- function(object, ...) 2 * ..1
> > foo.numeric.numeric <- function(object, ...) ..1 + ..2
> >
> > ______________________________________________
> > R-devel at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>
>--
>best,
>-tony
>
>"Commit early,commit often, and commit in a repository from which we can 
>easily
>roll-back your mistakes" (AJR, 4Jan05).
>
>A.J. Rossini
>blindglobe at gmail.com



More information about the R-devel mailing list