[R] How to get setGeneric() to work?

John Chambers jmc at research.bell-labs.com
Mon Jan 7 15:10:23 CET 2002


Henrik Bengtsson wrote:
> 
> Dear all, I can't see how setGeneric() works. 

setGeneric initializes the generic function.  It doesn't have to be
called if you want the current, ordinary function of the same name to
become the default method (not a bad idea, even if you want that method
to generate an error message, as in this example).

See the examples starting on page 348 of "Programming with Data", which
also explains when you might want to call setGeneric.

If you DO use setGeneric with an explicit definition, you're asserting
that that definition does method dispatch somewhere in the body of the
function (again, see the examples in the book).  The call in your
example doesn't do any method dispatch (no call to standardGeneric).

If you wanted to create getArea as a function with no default method,
that can be done by:

   setGeneric("getArea", function(object)standardGeneric("getArea"))

If you then call setMethod for class "Square", things will go as before.


>  I know perfectly well how to
> define
> 
>  library(methods);
> 
>  setClass("Square", representation(side="numeric"));
> 
>  # Will become the default method.
>  getArea <- function(object) {
>    stop(paste("Method getArea() is not defined for this class:",
>       data.class(object)));
>  }
> 
>  setMethod("getArea", "Square", function(object) object at side^2);
> 
>  # TEST
>  sq <- Square(8);
>  area <- getArea(sq);
> 
> However, when use setGeneric() explicitly without defining getArea();
> 
>  setGeneric("getArea", function(object) {
>    stop(paste("Method getArea() is not defined for this class:",
>               data.class(object)));
>  })
> 
> the getArea(sq) call will give:
> 
>  "Error in getArea(sq) : Method getArea() is not defined for this class:
> Square"
> 
> which is not what I expected (I would expect the same as the code above). I
> believe I miss something, but what?
> 
> Thanks
> 
> Henrik Bengtsson
> 
> Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
> Lund Institute of Technology/Lund University, Sweden (+2h UTC)
> Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
> h b @ m a t h s . l t h  . s e
> http://www.maths.lth.se/matstat/staff/hb/
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help 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-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list