[R] Making a new generic {was "Save method"}

Henrik Bengtsson henrikb at braju.com
Mon Nov 11 01:47:46 CET 2002


Dear all, I have been struggling to find a way of creating generic
functions in a safe way that do not make any assumptions on what
packages have been loaded before my packages are loaded. For this reason
there needs to be some conditioning involved when you generate a generic
function, e.g. a test whether a generic function and/or a default
function already exist or not. In the end I ended up writing a
setGenericS3() method, which does all these checks for you and also
checks some other things. I have been using it for more than one year
now in several different packages and bundles and it seems to work
pretty good. For the source code and for a detailed discussion about
creating generic functions, see
http://www.maths.lth.se/help/R/setGenericS3/. Please feel free to
comment on this and/or to improve the code.

Cheers

Henrik Bengtsson

Centre for Mathematical Sciences
Lund University


> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch 
> [mailto:owner-r-help at stat.math.ethz.ch] On Behalf Of Martin Maechler
> Sent: den 8 november 2002 19:34
> To: Ernesto Jardim
> Cc: Mailing List R
> Subject: [R] Making a new generic {was "Save method"}
> 
> 
> >>>>> "Ernesto" == Ernesto Jardim <ernesto at ipimar.pt>
> >>>>>     on 07 Nov 2002 15:43:02 +0000 writes:
> 
>     Ernesto> I want to use save methods but I found that the
>     Ernesto> save function is not a generic function ...
> 
>     Ernesto> If I define 
> 
>     Ernesto> save <- function(x, ...)
>     Ernesto> UseMethod("save")
> 
>     Ernesto> I'll be overwriting the original save function ...
> 
>     Ernesto> Is there another way of doing this ? or am I
>     Ernesto> understanding it wrong ?
> 
> ``As always'' you find the answer to this question in the 
> "R Extensions" / "Writing R Extensions (including C & 
> Fortran)" manual.  Look for the section  ``Adding new 
> generics''. Yes, please read that in the full context (you'll 
> learn things that you should rather learn now than later).
> 
> But for the sake of .., the crucial excerpt is
> 
>  RExt>    Sometimes package writers want to make generic a 
> function in the base
>  RExt> package, and request a change in R.  This may be 
> justifiable, but  RExt> making a function generic with the 
> old definition as the default method  RExt> does have a small 
> performance cost.  It is never necessary, as a package  RExt> 
> can take over a function in the base package and make it 
> generic by  RExt> 
>  RExt>      foo <- function(object, ...) UseMethod("foo")
>  RExt>      foo.default <- get("foo", pos = NULL, mode = "function")
> 
> 
> Martin Maechler <maechler at stat.math.ethz.ch>	
http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._



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