[R] setMethod confusion

Henrik Bengtsson hb at maths.lth.se
Wed Mar 15 11:59:02 CET 2006


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)




More information about the R-help mailing list