[R] S4: Initialization method called during setClass??

Martin Morgan mtmorgan at fhcrc.org
Fri Jun 5 18:49:21 CEST 2009


Hi Vitalie --

Vitalie S. wrote:
> Dear UseRs,
> 
> A simple class inheritance example:
> 
>> setClass("test",representation(a="numeric"))
>> setMethod("initialize","test",
>           function(.Object,x,...){
>               print("Initialization!!!")
>               callNextMethod(.Object,a=x,...)
>           })
> 
>> new("test",x=23)
> [1] "Initialization!!!"
> An object of class "test"
> Slot "a":
> [1] 23

the implicit contract is that the initialize method is written so that
new("test") works. This contract comes up at several points during class
definition and instantiation. You've identified one of those points.

> 
>> setClass("test2",contains="test",representation(b="integer"))
> [1] "Initialization!!!"
> Error in .nextMethod(.Object, a = x, ...) : argument "x" is missing,
> with no default

I'm not sure of the details, but part of the class definition is a
coercion method, and in defining this new() is called, without
arguments, on the contained classes.

Martin

> 
> 
> When trying to define a new class "test2" above, "initialize" method for
> "test" is called (??? looks like a bug to me) and naturally argument "x"
> is not found. Thus I can not create a subclass for "test".
> I could not find anything in documentation.
> Thanks,
> Vitalie.
> 
> ______________________________________________
> 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