[R] generic default values

Martin Maechler maechler at stat.math.ethz.ch
Tue Oct 2 17:31:42 CEST 2001


>>>>> "PaulG" == Paul Gilbert <pgilbert at bank-banque-canada.ca> writes:

    PaulG> I have a generic function test.equal <- function(obj1, obj2,
    PaulG> ...) UseMethod("test.equal")

    PaulG> however, the only argument that specific methods use is "fuzz"
    PaulG> so I would like to change this to

    PaulG> test.equal <- function(obj1, obj2, fuzz=???)
    PaulG> UseMethod("test.equal")

    PaulG> The problem is that some methods use slightly different default
    PaulG> values for fuzz than others. How should the generic be specified
    PaulG> to allow this?

I think ``anything goes'' since the default is taken from the method, not
the generic anyway.  
I would use "NULL" , as in the following example :

  > test.equal <- function(obj1, obj2, fuzz= NULL) UseMethod("test.equal")

  > test.equal.foo <- function(obj1, obj2, fuzz= 1e-10) {
  +     cat("`test.equal.foo(*): fuzz =", format(fuzz),"\n")
  +     fuzz
  + }    

  > foo <- function(x) { y <- x; class(y) <- "foo"; y }

  > test.equal(foo(1), foo(2))
  `test.equal.foo(*): fuzz = 1e-10 
  [1] 1e-10
  > 

BTW: In R-devel (which you seem to be using, Paul), i.e., from R version 1.4,
     (as in S version 4, and by the same author!),
     there is the generic function

  identical()
 
for comparing objects. Of course, ``identical'' implies that no fuzz can be
used. 

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list