Dear list, 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 mathematicalfunction 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. I'm pretty clueless after having studied most of the material in the documentation of the methods package and single functions concerning generic behavior -- somehow I don't see what I'm doing wrong. Any help would be greatly appreciated, Greetings, Ockham
The method below looks for arguments of "sec" and "numeric".
Sounds like
you want to pass in "sec" and "missing" (the class of
missing data) and
get a default behavior. So in addition to the method you already wrote,
try this one as well:
setMethod("signif", signature(x = "sec", digits =
"missing"),
function(x, digits) {
digits <- 2
## .. do something
print(digits)
}
)
Now we get the default behavior:> signif(new("sec"))
[1] 2
Robert
-----Original Message-----
From: ockham at gmx.net [mailto:ockham at gmx.net]
Sent: Tuesday, January 18, 2005 7:50 PM
To: r-help at stat.math.ethz.ch
Subject: [R] signif() generic
Dear list,
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. I'm pretty
clueless after having studied most of the material in the documentation
of the methods package and single functions concerning generic behavior
-- somehow I don't see what I'm doing wrong.
Any help would be greatly appreciated,
Greetings,
Ockham
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
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)