SetGeneric and formula interfaces

Torsten Hothorn Torsten.Hothorn@rzmail.uni-erlangen.de
Mon, 1 Jul 2002 12:17:31 +0200 (MEST)


Hi, 

while playing with the `methods' package I was not able to find what the
recommended way of dealing with the following problem is.

Suppose there is a generic function `foo' and two functions `foo.default'
and `foo.formula', in the classical way:

foo <- function(y, ...) UseMethod("foo")

foo.default <- function(y, ...) {}

foo.formula <- function(formula, data, subset, na.action, ...) {
 ...
 do.call("foo", y=something appropriate,...)
}

If I recall it correctly, foo.formula is the only function that is allowed
to have different arguments than the generic function has (at least by R
CMD check).

Now, using `setGeneric' and `setMethod': 

R> library(methods)
R> 
R> setGeneric("foo", function(y, ...) standardGeneric("foo"))
[1] "foo"
R> 
R> setMethod("foo", signature(y="numeric"),
+ function(y, ...) print("foo with numeric"))
[1] "foo"
R> 
R> setMethod("foo", signature(y="formula"),
+ function(y, ...) print("foo with formula"))
[1] "foo"
R> 
R> foo(x ~ a)
[1] "foo with formula"
R> foo(1:10)
[1] "foo with numeric"

fine. But is it possible to have a method that has an argument `formula'
instead of `y', at least for the sake of compatibility with the classical
way? 

Torsten

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._