From davide.rambaldi at ieo.eu  Thu Dec 13 12:48:37 2012
From: davide.rambaldi at ieo.eu (Davide Rambaldi)
Date: Thu, 13 Dec 2012 12:48:37 +0100
Subject: [Rd] Lost in S4 and S3 classes
In-Reply-To: <D5F5DD5E-9BA1-4C15-B943-E6AC21300D4F@ieo.eu>
References: <D5F5DD5E-9BA1-4C15-B943-E6AC21300D4F@ieo.eu>
Message-ID: <6424B597-0E06-4FD0-83B7-D498C48176EC@ieo.eu>

Hi, for the r-devel archives, I report here the answer of Martin Morgan on stackoverflow (http://goo.gl/CF5I9):

Actually, the no-argument constructor returns an invalid object, too, it's just 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, maybe

setClass (
  
Class="TestClass",

  representation=representation(
      lmOutput = "nls.lm",
      anumeric = "numeric"
),

  prototype=prototype(
      lmOutput=structure(list(), class="nls.lm")
)
)

Answered by Martin Morgan


All the best

Davide R.



On Dec 12, 2012, at 2:51 PM, Davide Rambaldi wrote:

> Hi all, this is my first post in R devel? sorry if I lost some of the guidelines. 
> 
> Anyway this is my problem:
> 
> Version:
> 
> R version 2.15.2 (2012-10-26)
> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
> 
> I want to make an S4 class that use the output object of the function of nls.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 (correct?):
> 
> 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 object "pippo" seems correctly initialized.
> 
> 
> If I use this code instead I got 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"
> 
> 
> Seems that if I want to INIT in new some slots, this create problem with a S3 Class slot?
> 
> Any clue on how to avoid this problem?
> 
> Thanks
> 
> 
> 
> 
> 
> -----------------------------------------------------------
> PLEASE NOTE MY NEW EMAIL ADDRESS
> -----------------------------------------------------------
> 
> -----------------------------------------------------
> Davide Rambaldi, Bioinformatics PhD.
> -----------------------------------------------------
> IEO ~ MolMed
> [e] davide.rambaldi at ieo.eu
> [e] davide.rambaldi at gmail.com
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Lost in S4 and S3 classes
Davide Rambaldi