[R] signif() generic
    Paul Roebuck 
    roebuck at odin.mdacc.tmc.edu
       
    Wed Jan 19 16:38:00 CET 2005
    
    
  
On Wed, 19 Jan 2005 ockham at gmx.net wrote:
> I'm trying to write a class for Gaussian error propagation of measured
> values and their (estimated) errors,
>
> > setClass("sec", representation(val="numeric", err="numeric"))
>
> I've already successfully implemented basic arithmetics using mostly the
> "Arith" group generics. But I'm running into trouble when trying to get
> signif() to work for my purposes, i.e. with a default argument
> (digits=2): When trying
>
> > seMethod("signif", signature(x="sec", digits="numeric")),
> 	function(x, digits=2){
> 	# ...do something...
> 	}
> )
>
> and
>
> > signif(sec1)
>
> I get
>
> > Error in signif(x, digits) : Non-numeric argument to mathematical
> function
>
> Putting a second argument (like digits=2) into the call makes it work,
> but I want some default behavior specified for missing digits argument
> so it works in an analogous fashion as signif for numeric values.
> I also tried inserting
>
> > setGeneric("signif", function(x, digits=6) standardGeneric("signif"))
>
> before the setMethod block, but that wouldn't help either.
setGeneric("signif", function(x, digits=6) standardGeneric("signif"))
setMethod("signif", signature(x="sec", digits="numeric"),
    function(x, digits) {
        # ...do something...
        print(x)
        print(digits)
    }
)
setMethod("signif", signature(x="sec", digits="missing"),
    function(x, digits) {
        callGeneric(x, digits)
    }
)
The missing data method will cause the default value (6) to
be passed to the numeric data method.
> signif(new("sec"))
An object of class "sec"
   ...
[1] 6
>
----------------------------------------------------------
SIGSIG -- signature too long (core dumped)
    
    
More information about the R-help
mailing list