[R] Seeking help to define method for '+'
Martin Morgan
mtmorgan at fhcrc.org
Sat Feb 5 23:26:59 CET 2011
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.
--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109
Location: M1-B861
Telephone: 206 667-2793
More information about the R-help
mailing list