[Rd] setMethod() woes

John Chambers jmc at r-project.org
Fri Sep 15 16:40:44 CEST 2006


The document on developer.r-project.org/howMethodsWork.pdf explains why 
the two methods are equally close.  Please (re)read it.

If you know what methods you want for mixing the two objects, you must 
either say so or arrange the classes in an inheritance that includes 
both classes.

Robin Hankin wrote:
> Hello everybody
>
> R version 2.4.0 alpha (2006-09-15 r39323), MacOSX 10.4.7
>
>
> Next S4 problem.
>
> I have "brob" objects that are large real numbers, and now I want "glub"
> numbers that are to be a pair of glubs that represent complex numbers.
>
> I want to define binary operator "+" so that if either the left or right
> argument are glubs,  it uses .ArithGlub.
>
> If either argument is a brob (but neither is a glub), use .ArithGlub
>   
You don't mean what you just said, presumably ".ArithBrob"

But this is not specifiable as a method signature unless you create a 
"brobOrGlub" class.  This is a Do What I Mean.
> If neither is a brob or a glub, use standard "+"
>
> (just like real/complex "+").
>
> My attempt (self contained, minimal) is included below.
> But, with this, the following session shows that something is wrong:
>
>
>
>  > a <- new("brob",x=1:5,positive=rep(T,5))
>  > b <- new("glub",real=a,imag=a)
>  > a+b
> Error in a + b : binary operator " + " not defined for Brobdingnagian  
> numbers
> In addition: Warning message:
> Ambiguous method selection for "+", target "brob#glub" (the first of  
> the signatures shown will be used)
>      brob#ANY
>      ANY#glub
> in: .findInheritedMethods(classes, fdef, mtable)
>
>
>
> I don't understand what to do to avoid getting the warning
> message [and it's also using  the wrong function: I want it
> to call .ArithGlub() here, not .ArithBrob() ]
>
> I need to use signature(e1="brob", e2="anything except a glub")
> but I can't figure out how to do this.
>
>
>
>
>
>
>
> setClass("brob",
>           representation = representation 
> (x="numeric",positive="logical"),
>           prototype      = list(x=numeric(),positive=logical())
>           )
>
> setClass("glub",
>           representation = representation(real="brob",imag="brob"),
>           prototype      = list(real=new("brob"), imag=new("brob"))
>           )
>
> .ArithBrob <- function(e1,e2){	
>    stop(paste("binary operator \"", .Generic, "\" not defined for  
> Brobdingnagian numbers"))	
> }		     	
>
> .ArithGlub <- function(e1,e2){	
>    stop(paste("binary operator \"", .Generic, "\" not defined for  
> Glub numbers"))
> }		     	
>
> setMethod("Arith",signature(e1 = "brob", e2="brob"), .ArithBrob)
> setMethod("Arith",signature(e1 = "brob", e2="ANY" ), .ArithBrob)
> setMethod("Arith",signature(e1 = "ANY" , e2="brob"), .ArithBrob)
>
> setMethod("Arith",signature(e1 = "glub"), .ArithGlub)
> setMethod("Arith",signature(e2 = "glub"), .ArithGlub)
>
>
>
>
>
>
>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
>   tel  023-8059-7743
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>




More information about the R-devel mailing list