[R] Seeking help to define method for '+'

Duncan Murdoch murdoch.duncan at gmail.com
Sat Feb 5 23:48:40 CET 2011


On 05/02/2011 5:58 PM, Bogaso Christofer wrote:
> Thanks Martin for your help. Although your suggestion is working for
> somewhat restrictive case, I would like to make thing more generic. For
> example:
>
> setMethod("Arith", c("Me", "Me"), function(e1, e2) callGeneric(e1 at x1,
> e2 at x1))
>
> with in (new1 + new2) is working perfectly, however if I want to add
> arbitrary number of objects then it fails:
>
>> new1 + new2 - new1
> Error in new1 + new2 - new1 : non-numeric argument to binary operator
>
> What can I do to handle this?

Your function returns a number, not a Me object, so R ends up with a
number minus a Me.    You could define what to do in that case,
or have your function construct a Me object and return that, in which 
case you'll only see the signature (Me, Me).  In the latter case you 
won't be able to handle things like (new1 + 1).

Duncan Murdoch

>
> Thanks and regards,
>
>
> -----Original Message-----
> From: Martin Morgan [mailto:mtmorgan at fhcrc.org]
> Sent: 06 February 2011 03:57
> To: Bogaso Christofer
> Cc: r-help at r-project.org
> Subject: Re: [R] Seeking help to define method for '+'
>
> On 02/05/2011 02:19 PM, Bogaso Christofer wrote:
>> Dear all, I am trying to define "+" method for my newly defined s4 class
>> which is as follows:
>>
>>
>>
>> setClass("Me", sealed=F,representation(x1 = "numeric", x2 = "character"))
>>
>> new1<- new("Me", x1=2, x2="comment1")
>>
>> new2<- new("Me", x1=3, x2="comment1")
>>
>>
>>
>> setMethod("+", "Me", definition=function(x,y) cat(x at x1 + y at x1, "\n"))
>>
>>
>>
>> However while am trying to set method, it fails:
>>
>> Error in conformMethod(signature, mnames, fnames, f, fdef, definition) :
>>
>>    in method for '+' with signature 'e1="Me"': formal arguments (e1 = "Me",
>> e2 = "Me") omitted in the method definition cannot be in the signature
>
> From
>
>> getGeneric("+")
> standardGeneric for "+" defined from package "base"
>    belonging to group(s): Arith
>
> function (e1, e2)
> standardGeneric("+", .Primitive("+"))
> <environment: 0x19a9918>
> Methods may be defined for arguments: e1, e2
> Use  showMethods("+")  for currently available ones.
>
> you can see that the generic is defined to dispatch on two arguments in
> a signature function(e1, e2). So
>
>    setMethod("+", c("Me", "Me"), function(e1, e2) e1 at x1 + e2 at x1)
>
> but S4 also has group generics, and
>
>    setMethod("Arith", c("Me", "Me"),
>        function(e2, e2) callGeneric(e1 at x1, e2 at x1))
>
> will define not just + but also - and other functions in the 'Arith'
> group. See
>
>    ?GroupGenericFunctions
>
> Martin
>
>>
>>
>>
>> I am just started learning s4 class system therefore please forgive me if
> I
>> have some any bad mistakes. Anyway I would really appreciate if someone
>> point me how should I proceed.
>>
>>
>>
>> Thanks and reagrds,
>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list