Use virtual class S3 as a slot of class S4, get an error: get the class "S4", there must be or extend the class "nls.lm",

R Version:

R version 2.15.2 (2012-10-26) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) 

I want to create an S4 class that uses the output object of the nls.lm function (package: minpack.lm) as a slot:

 setOldClass("nls.lm") setClass ( Class="TestClass", representation=representation( lmOutput = "nls.lm", anumeric = "numeric" ) ) 

Now, if I want to call this class in a "constructor function", I can do something like this (right?):

 myConstructor <- function() { return(new("TestClass")) } pippo <- myConstructor() pippo An object of class "TestClass" Slot "lmOutput": <S4 Type Object> attr(,".S3Class") [1] "nls.lm" Slot "anumeric": numeric(0) 

And the pippo object looks properly initialized.

If I use this code, I get an error:

 myConstructor2 <- function() { pippo <- new("TestClass", anumeric=1000) return(pippo) } pippo <- myConstructor2() Error in validObject(.Object) : invalid class "TestClass" object: invalid object for slot "lmOutput" in class "TestClass": got class "S4", should be or extend class "nls.lm" 

It seems that if I want INIT in the new slots, this will create a problem with the class S3 slot?

Is there a clue on how to avoid this problem?

thanks

+6
source share
1 answer

Actually, the constructor without arguments returns an invalid object, it just was not tested

 > validObject(new("TestClass")) Error in validObject(new("TestClass")) : invalid class "TestClass" object: invalid object for slot "lmOutput" in class "TestClass": got class "S4", should be or extend class "nls.lm" 

The solution is to provide an appropriate prototype, perhaps

 setClass ( Class="TestClass", representation=representation( lmOutput = "nls.lm", anumeric = "numeric" ), prototype=prototype( lmOutput=structure(list(), class="nls.lm") ) ) 
+3
source

All Articles