Hi, when defining method (I used length bellow just for the simplicity) for myClass (S4 class) I use setMethod(f="length", signature(x="myClass"), def=function(x) { cat("works") }) If I have myClass1 and myClass2 and mentioned method, which is not the default one, applies to both classes I have to write two setMethod() statements. Would it be possible to use signature(x=c("myClass1", "myClass2")) Currently it issues an error setClass(Class="myClass1", representation=representation(x="integer")) setClass(Class="myClass2", representation=representation(x="integer")) setMethod(f="length", signature(x="myClass1"), def=function(x) { cat("works") }) ## OK. setMethod(f="length", signature(x=c("myClass1", "myClass1")), def=function(x) { cat("works") }) Error in signature(x = c("myClass1", "myClass1")) : bad class specified for element 1 (should be a single character string) Regards, Gregor
On Thursday 01 March 2007 05:47, Gregor Gorjanc wrote:> Hi, > > when defining method (I used length bellow just for the simplicity) for > myClass (S4 class) I use > > setMethod(f="length", signature(x="myClass"), > def=function(x) { > cat("works") > }) > > If I have myClass1 and myClass2 and mentioned method, which is not the > default one, applies to both classes I have to write two setMethod() > statements. Would it be possible to use > > signature(x=c("myClass1", "myClass2"))Could you do: catFun <- function(x) { cat("Works") } setMethod(f="length", signature(x="myClass1"),def=catFun) setMethod(f="length", signature(x="myClass2"),def=catFun) Sean
On Thursday 01 March 2007 06:18, Sean Davis wrote:> On Thursday 01 March 2007 05:47, Gregor Gorjanc wrote: > > Hi, > > > > when defining method (I used length bellow just for the simplicity) for > > myClass (S4 class) I use > > > > setMethod(f="length", signature(x="myClass"), > > def=function(x) { > > cat("works") > > }) > > > > If I have myClass1 and myClass2 and mentioned method, which is not the > > default one, applies to both classes I have to write two setMethod() > > statements. Would it be possible to use > > > > signature(x=c("myClass1", "myClass2")) > > Could you do: > > catFun <- function(x) { > cat("Works") > } > setMethod(f="length", signature(x="myClass1"),def=catFun) > setMethod(f="length", signature(x="myClass2"),def=catFun)That said, it your myclass1 and myclass2 are related, you may want to make them both subclasses of a virtual class. You could then use that virtual class in the signature or anywhere you wanted common functionality for two or more classes. Sean
Sean Davis wrote: ...> > Could you do: > > catFun <- function(x) { > cat("Works") > } > setMethod(f="length", signature(x="myClass1"),def=catFun) > setMethod(f="length", signature(x="myClass2"),def=catFun)Yep, that is what I wanted. setMethod(f="length", signature(x=c("myClass1", "myClass2")) would be shorter, but your proposal is also OK. Thank you again, Gregor