On 02/05/2011 02: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@x1, > e2@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
as written, new1 + new2 returns a numeric, so (new1 + new2) - new1 would require a method with signature c("numeric", "Me"). But perhaps what you want is an implementation along the lines of setMethod(Arith, c("Me", "Me"), function(e1, e2) { new("Me", x1=callGeneric(e1@x1, e2@x2) x2=c(e1@x2, e2@x2)) }) i.e., return an instance of "Me". Martin > > What can I do to handle this? > > Thanks and regards, > > > -----Original Message----- > From: Martin Morgan [mailto:mtmor...@fhcrc.org] > Sent: 06 February 2011 03:57 > To: Bogaso Christofer > Cc: r-help@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@x1 + y@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@x1 + e2@x1) > > but S4 also has group generics, and > > setMethod("Arith", c("Me", "Me"), > function(e2, e2) callGeneric(e1@x1, e2@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@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. > > -- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793 ______________________________________________ R-help@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.