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

Henrik Bengtsson hb at maths.lth.se
Mon Jan 7 20:38:28 CET 2002


> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of John Chambers
> Sent: Monday, January 07, 2002 3:10 PM
> To: Henrik Bengtsson
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] How to get setGeneric() to work?
>
>
> 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 totally missunderstood what standardGeneric() did; I though it generated a
default method for getArea (I am stuck in the S3/UseMethod way of think). My
misstake and thanks for clearing it out. I tried to backtrack why I
missunderstood it and reading help page for GenericFunctions (setGeneric and
standardGeneric etc);

`setGeneric': If there is already a non-generic function of this
          name, it will be used to define the generic unless `def' is
          supplied, and the current function will become the default
          method for the generic.

          If `def' is supplied, this defines the generic function, and
          no default method will exist (often a good feature, if the
          function should only be available for a meaningful subset of
          all objects).
...
`standardGeneric': Generic functions should usually have a call to
          `standardGeneric' as their entire body.  They can, however,
          do any other computations as well.

          The usual `setGeneric' (directly or through calling
          `setMethod') creates a function with a call to
          `standardGeneric'.

I believe that I interpreted the "If there is already a non-generic function
of this name, it will be used to define the generic" (together with the last
sentence of standardGeneric) to say that by doing setGeneric("foo", def=foo)
the function foo would be called as the default method. Do you see where it
could be misunderstood? Maybe a clarifaction is needed?

What I wanted to do in my initial message was:

 setGeneric("getArea", function(object) standardGeneric("getArea"));
 setMethod("getArea", "ANY", function(object) {
   stop(paste("Method getArea() is not defined for this class:",
              data.class(object)));
 })

I didn't though about using "ANY" as the ONLY argument in the signature. (A
method defined with signature equal to "ANY" is, similar to the
getArea.default function when using UseMethod (S3 style) method dispatching,
called when no other signature is found to match the call).

> >  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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._


I appreciate your help!

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list