S4 default initialization: unwanted NULL

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

S4 default initialization: unwanted NULL

Seth Falcon-2
The default initialization for slots of class "factor" and
"data.frame" gives NULL.  This seems odd, since those slots can't ever
be set to NULL by the user.  I would expect zero-length instances of
factor and data.frame.

Here is an example:

setClass("FOO", representation(a="factor", b="data.frame", c="numeric"))
[1] "FOO"
> ff <- new("FOO")
> ff
An object of class "FOO"
Slot "a":
NULL

Slot "b":
NULL

Slot "c":
numeric(0)


sessionInfo()
R version 2.3.0, 2005-12-26, powerpc-apple-darwin8.3.0

attached base packages:
[1] "tools"     "methods"   "stats"     "graphics"  "grDevices" "utils"    
[7] "datasets"  "base"    


Slot c is initialized as I was expecting.

+ seth

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: S4 default initialization: unwanted NULL

Prof. Dr. Matthias Kohl
you might need a call to "setOldClass"; see Section "Register or
Convert?" of the corresponding help page.

Matthias

Seth Falcon schrieb:

>The default initialization for slots of class "factor" and
>"data.frame" gives NULL.  This seems odd, since those slots can't ever
>be set to NULL by the user.  I would expect zero-length instances of
>factor and data.frame.
>
>Here is an example:
>
>setClass("FOO", representation(a="factor", b="data.frame", c="numeric"))
>[1] "FOO"
>  
>
>>ff <- new("FOO")
>>ff
>>    
>>
>An object of class "FOO"
>Slot "a":
>NULL
>
>Slot "b":
>NULL
>
>Slot "c":
>numeric(0)
>
>
>sessionInfo()
>R version 2.3.0, 2005-12-26, powerpc-apple-darwin8.3.0
>
>attached base packages:
>[1] "tools"     "methods"   "stats"     "graphics"  "grDevices" "utils"    
>[7] "datasets"  "base"    
>
>
>Slot c is initialized as I was expecting.
>
>+ seth
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-devel
>  
>


--
StaMatS - Statistik + Mathematik Service
Dipl.Math.(Univ.) Matthias Kohl
Gottlieb-Keim-Stra├če 60
95448 Bayreuth
Phone: +49 921 50736 457
E-Mail: [hidden email]
www.stamats.de

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: S4 default initialization: unwanted NULL

John Chambers-2
S3 classes are treated as virtual classes, with or without a call to
setOldClass()--the purpose of setOldClass() is to make method dispatch
with S3 inheritance work.

It's legal to have virtual classes as slots, but yes, the slot is NULL
in the prototype for the new class, unless the user specifies a value.  
In your case, providing a non-null prototype for the data.frame slot
might be the desired solution.

There is no S4 "initialization" for S3 classes; in fact, it's generally
an error to use new() on them (or on other virtual classes).

 > getClass("data.frame")
Virtual Class

No Slots, prototype of class "NULL"

Extends: "oldClass"

Known Subclasses: "anova"

 > new("data.frame")
Error in new("data.frame") : trying to use new() on a virtual class


John

PS: it's possible to imagine an extended version of setOldClass() that
made S3 classes into non-virtual S4 classes.  Worth discussion, but a
definite change to current design.


Matthias Kohl wrote:

>you might need a call to "setOldClass"; see Section "Register or
>Convert?" of the corresponding help page.
>
>Matthias
>
>Seth Falcon schrieb:
>
>  
>
>>The default initialization for slots of class "factor" and
>>"data.frame" gives NULL.  This seems odd, since those slots can't ever
>>be set to NULL by the user.  I would expect zero-length instances of
>>factor and data.frame.
>>
>>Here is an example:
>>
>>setClass("FOO", representation(a="factor", b="data.frame", c="numeric"))
>>[1] "FOO"
>>
>>
>>    
>>
>>>ff <- new("FOO")
>>>ff
>>>  
>>>
>>>      
>>>
>>An object of class "FOO"
>>Slot "a":
>>NULL
>>
>>Slot "b":
>>NULL
>>
>>Slot "c":
>>numeric(0)
>>
>>
>>sessionInfo()
>>R version 2.3.0, 2005-12-26, powerpc-apple-darwin8.3.0
>>
>>attached base packages:
>>[1] "tools"     "methods"   "stats"     "graphics"  "grDevices" "utils"    
>>[7] "datasets"  "base"    
>>
>>
>>Slot c is initialized as I was expecting.
>>
>>+ seth
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>>    
>>
>
>
>  
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: S4 default initialization: unwanted NULL

Seth Falcon-2
In reply to this post by Prof. Dr. Matthias Kohl
On  3 Jan 2006, [hidden email] wrote:

> you might need a call to "setOldClass"; see Section "Register or
> Convert?" of the corresponding help page.

That doesn't seem to make a difference.  Note that other non-S4 slots
like "matrix" initialize in an appropriate (non-NULL) fashion.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: S4 default initialization: unwanted NULL

Seth Falcon-2
In reply to this post by John Chambers-2
On  3 Jan 2006, [hidden email] wrote:
> It's legal to have virtual classes as slots, but yes, the slot is
> NULL in the prototype for the new class, unless the user specifies a
> value.  In your case, providing a non-null prototype for the
> data.frame slot might be the desired solution.

Yes, that's a workaround.  

> There is no S4 "initialization" for S3 classes; in fact, it's
> generally an error to use new() on them (or on other virtual
> classes).

The "basic vector classes" (see man page for new) can be created
with new().  From my perspective it would be more consistent if
data.frame and factor behaved similarly.  

I admit that I don't understand why data.frame and factor are virtual
classes and don't know what would be involved to have new() work for
data.frame and factor.

+ seth

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: S4 default initialization: unwanted NULL

John Chambers-2
Seth Falcon wrote:

>On  3 Jan 2006, [hidden email] wrote:
>  
>
>>It's legal to have virtual classes as slots, but yes, the slot is
>>NULL in the prototype for the new class, unless the user specifies a
>>value.  In your case, providing a non-null prototype for the
>>data.frame slot might be the desired solution.
>>    
>>
>
>Yes, that's a workaround.  
>
>  
>
>>There is no S4 "initialization" for S3 classes; in fact, it's
>>generally an error to use new() on them (or on other virtual
>>classes).
>>    
>>
>
>The "basic vector classes" (see man page for new) can be created
>with new().  From my perspective it would be more consistent if
>data.frame and factor behaved similarly.  
>  
>
The basic classes are set up specially, as "real" S4 classes.  In a
sense, it would be more consistent NOT to do this & have them be S3
classes as well, but I assume you're not suggesting that ;-)

The reason that S3 classes are generally virtual is simple:  there is no
metadata information to tell the system anything at all about the
objects from the class, and in particular what a prototype object would
be.  Therefore, new() doesn't know what to return.

In a future version, we could augment setOldClass() to provide such
information, but it remains true that some S3 classes don't behave like
legal S4 classes (e.g., different objects from the class can have
different "slots").

>I admit that I don't understand why data.frame and factor are virtual
>classes and don't know what would be involved to have new() work for
>data.frame and factor.
>
>+ seth
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-devel
>
>  
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel