Stephen Henderson
2006-Mar-15 12:28 UTC
[R] setMethod confusion -best reference for S4 programming
Thanks I think you have both answered my question (reckon Ill go S3 on that). As an adjunct to this do you know what might be the best reference to the S4 methods current implementation. I have ordered the Chambers book "Programming with Data", and I have a short tutorial-- "S4 Classes in 15 pages, more or less". Have I missed any other useful resources? Stephen Henderson Wolfson Inst. for Biomedical Research Cruciform Bldg., Gower Street University College London United Kingdom, WC1E 6BT +44 (0)207 679 6827 -----Original Message----- From: henrik.bengtsson at gmail.com [mailto:henrik.bengtsson at gmail.com] On Behalf Of Henrik Bengtsson Sent: 15 March 2006 10:59 To: Martin Maechler Cc: Stephen Henderson; r-help at stat.math.ethz.ch Subject: Re: [R] setMethod confusion On 3/15/06, Martin Maechler <maechler at stat.math.ethz.ch> wrote:> >>>>> "Stephen" == Stephen Henderson <s.henderson at ucl.ac.uk> > >>>>> on Tue, 14 Mar 2006 16:32:56 -0000 writes: > > Stephen> Hello I've checked through previous postings but > Stephen> don't see a fully equivalent problem-just a few > Stephen> hints. I have been trying to set a new method for > Stephen> the existing function "table" or > Stephen> "as.data.frame.table" for my class "tfSites". > Stephen> Taking out all the useful code and just returning > Stephen> the input class I get the error > > >> setMethod("table", "tfSites", function(.Object) .Object) > > Stephen> Error in conformMethod(signature, mnames, fnames, > Stephen> f) : In method for function "table": formal > Stephen> arguments omitted in the method definition cannot > Stephen> be in the signature (exclude = "tfSites") > > > >> setMethod("as.data.frame.table", "tfSites", > >> function(.Object) .Object ) > > Stephen> Error in conformMethod(signature, mnames, fnames, > Stephen> f) : In method for function "as.data.frame.table": > Stephen> formal arguments omitted in the method definition > Stephen> cannot be in the signature (x = "tfSites") > > Stephen> What does this mean? Is there something peculiar > Stephen> about the table function? Is it because it takes > Stephen> arguments beginning table(..., etc) > > Yes. Since table's argument list starts with "..." > you cannot directly write S4 methods for it.Although not fully tested, but a workaround could be to i) define an S4 method tableS4(), then ii) rename table() to table.default() and iii) make table() an S3 generic function, and finally iv) define table.tfSites() to call tableS4(). Would this work? If so, step (ii)-(iv) can be done in one step using the R.oo package. Example: library(R.oo) setClass("fSites", representation(x="numeric", y="numeric")) setGeneric("tableS4", function(.Object, ...) standardGeneric("tableS4")) setMethod("tableS4", "fSites", function(.Object) .Object) setMethodS3("table", "fSites", function(.Object, ...) tableS4(.Object, ...)) Test;> x <- new("fSites") > table(x)An object of class "fSites" Slot "x": numeric(0) Slot "y": numeric(0)> X <- rpois(20, 1) > table(X)X 0 1 2 3 10 7 2 1 But, what's wrong with S3 in the first place? ;) /Henrik> One could consider changing table's argument list to become > (x, ..., exclude = c(NA, NaN), dnn = list.names(...), deparse.level= 1)> but that's not entirely trivial to do back compatibly, since > table() produces *named* dimnames from its arguments in "..." > and we'd want to make sure that this continues to work as now > even when the first argument is formally named 'x'. E.g., > > > X <- rpois(20, 1) > > table(X) > X > 0 1 2 3 > 7 10 2 1 > > > > should continue to contain "X" as names(dimnames(.)). > > Of course this has now become a topic for R-devel rather > than R-help. > > Martin Maechler, > ETH Zurich > > ______________________________________________ > 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> >-- Henrik Bengtsson Mobile: +46 708 909208 (+1h UTC) ********************************************************************** This email and any files transmitted with it are confidentia...{{dropped}}
Berton Gunter
2006-Mar-15 16:34 UTC
[R] setMethod confusion -best reference for S4 programming
You might also wish to read the relevant chapter of V&R's S PROGRAMMING. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Stephen Henderson > Sent: Wednesday, March 15, 2006 4:28 AM > To: r-help at stat.math.ethz.ch > Cc: Henrik Bengtsson; Martin Maechler > Subject: Re: [R] setMethod confusion -best reference for S4 > programming > > Thanks I think you have both answered my question (reckon Ill go S3 on > that). As an adjunct to this do you know what might be the best > reference to the S4 methods current implementation. > > I have ordered the Chambers book "Programming with Data", and I have a > short tutorial-- "S4 Classes in 15 pages, more or less". > > Have I missed any other useful resources? > > Stephen Henderson > Wolfson Inst. for Biomedical Research > Cruciform Bldg., Gower Street > University College London > United Kingdom, WC1E 6BT > +44 (0)207 679 6827 > > > -----Original Message----- > From: henrik.bengtsson at gmail.com > [mailto:henrik.bengtsson at gmail.com] On > Behalf Of Henrik Bengtsson > Sent: 15 March 2006 10:59 > To: Martin Maechler > Cc: Stephen Henderson; r-help at stat.math.ethz.ch > Subject: Re: [R] setMethod confusion > > On 3/15/06, Martin Maechler <maechler at stat.math.ethz.ch> wrote: > > >>>>> "Stephen" == Stephen Henderson <s.henderson at ucl.ac.uk> > > >>>>> on Tue, 14 Mar 2006 16:32:56 -0000 writes: > > > > Stephen> Hello I've checked through previous postings but > > Stephen> don't see a fully equivalent problem-just a few > > Stephen> hints. I have been trying to set a new method for > > Stephen> the existing function "table" or > > Stephen> "as.data.frame.table" for my class "tfSites". > > Stephen> Taking out all the useful code and just returning > > Stephen> the input class I get the error > > > > >> setMethod("table", "tfSites", function(.Object) .Object) > > > > Stephen> Error in conformMethod(signature, mnames, fnames, > > Stephen> f) : In method for function "table": formal > > Stephen> arguments omitted in the method definition cannot > > Stephen> be in the signature (exclude = "tfSites") > > > > > > >> setMethod("as.data.frame.table", "tfSites", > > >> function(.Object) .Object ) > > > > Stephen> Error in conformMethod(signature, mnames, fnames, > > Stephen> f) : In method for function "as.data.frame.table": > > Stephen> formal arguments omitted in the method definition > > Stephen> cannot be in the signature (x = "tfSites") > > > > Stephen> What does this mean? Is there something peculiar > > Stephen> about the table function? Is it because it takes > > Stephen> arguments beginning table(..., etc) > > > > Yes. Since table's argument list starts with "..." > > you cannot directly write S4 methods for it. > > Although not fully tested, but a workaround could be to i) define an > S4 method tableS4(), then ii) rename table() to table.default() and > iii) make table() an S3 generic function, and finally iv) define > table.tfSites() to call tableS4(). Would this work? If so, step > (ii)-(iv) can be done in one step using the R.oo package. Example: > > library(R.oo) > setClass("fSites", representation(x="numeric", y="numeric")) > setGeneric("tableS4", function(.Object, ...) > standardGeneric("tableS4")) > setMethod("tableS4", "fSites", function(.Object) .Object) > setMethodS3("table", "fSites", function(.Object, ...) tableS4(.Object, > ...)) > > Test; > > x <- new("fSites") > > table(x) > An object of class "fSites" > Slot "x": > numeric(0) > > Slot "y": > numeric(0) > > > X <- rpois(20, 1) > > table(X) > X > 0 1 2 3 > 10 7 2 1 > > But, what's wrong with S3 in the first place? ;) > > /Henrik > > > > > > One could consider changing table's argument list to become > > (x, ..., exclude = c(NA, NaN), dnn = list.names(...), > deparse.level > = 1) > > but that's not entirely trivial to do back compatibly, since > > table() produces *named* dimnames from its arguments in "..." > > and we'd want to make sure that this continues to work as now > > even when the first argument is formally named 'x'. E.g., > > > > > X <- rpois(20, 1) > > > table(X) > > X > > 0 1 2 3 > > 7 10 2 1 > > > > > > > should continue to contain "X" as names(dimnames(.)). > > > > Of course this has now become a topic for R-devel rather > > than R-help. > > > > Martin Maechler, > > ETH Zurich > > > > ______________________________________________ > > 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 > > > > > > > -- > Henrik Bengtsson > Mobile: +46 708 909208 (+1h UTC) > > ********************************************************************** > This email and any files transmitted with it are > confidentia...{{dropped}} > > ______________________________________________ > 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 >