Abby Spurdle
2020-Sep-23 21:18 UTC
[Rd] Is it possible to simply the use of NULL slots (or at least improve the help files)?
As far as I can tell, there's no trivial way to set arbitrary S4 slots to NULL. Most of the online examples I can find, use setClassUnion and are about 10 years old. Which, in my opinion, is defective. There's nothing "robust" about making something that should be trivially simple, really complicated. Maybe there is a simpler way, and I just haven't worked it out, yet. But either way, could the documentation for the methods package be improved? I can find any obvious info on NULL slots: Introduction Classes Classes_Details setClass slot Again, maybe I missed it. Even setClassUnion, which is what's used in the online examples, doesn't contain a NULL slot example. One more thing: The help file for setClassUnion, uses the term "superclass", incorrectly. Its examples include the following: setClassUnion("maybeNumber", c("numeric", "logical")) If maybeNumber was the superclass of numeric, then every instance of numeric would also be an instance of maybeNumber...
Abby Spurdle
2020-Sep-24 02:44 UTC
[Rd] Is it possible to simply the use of NULL slots (or at least improve the help files)?
Sorry, the title should be "simplify", and the third paragraph should say "I can't". (Don't know how I missed these).
Martin Morgan
2020-Sep-24 14:01 UTC
[Rd] Is it possible to simply the use of NULL slots (or at least improve the help files)?
I did ?"NULL<tab> at the command line and was lead to ?"NULL-class" and the BasicClasses help page in the methods package. getClass("NULL"), getClass("character") show that these objects are unrelated, so a class union is the way to define a class that is the union of these. The essence of the behavior you would like is setClassUnion("character_OR_NULL", c("character", "NULL")) .A = setClass("A", slots = c(x = "character_OR_NULL")) with> .A(x = NULL)An object of class "A" Slot "x": NULL> .A(x = month.abb)An object of class "A" Slot "x": [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"> .A(x = 1:5)Error in validObject(.Object) : invalid class "A" object: invalid object for slot "x" in class "A": got class "integer", should be or extend class "character_OR_NULL" I understand there are situations where NULL is desired, perhaps to indicate 'not yet initialized' and distinct from character(0) or NA_character_, but want to mention those often appropriate alternatives. With setClassUnion("maybeNumber", c("numeric", "logical")) every instance of numeric _is_ a maybeNumber, e.g.,> is(1, "maybeNumber")[1] TRUE> is(1L, "maybeNumber")[1] TRUE> is(numeric(), "maybeNumber")[1] TRUE> is(NA_integer_, "maybeNumber")[1] TRUE which I think is consistent with the use of 'superclass' on the setClassUnion help page. Martin Morgan ?On 9/23/20, 5:20 PM, "R-devel on behalf of Abby Spurdle" <r-devel-bounces at r-project.org on behalf of spurdle.a at gmail.com> wrote: As far as I can tell, there's no trivial way to set arbitrary S4 slots to NULL. Most of the online examples I can find, use setClassUnion and are about 10 years old. Which, in my opinion, is defective. There's nothing "robust" about making something that should be trivially simple, really complicated. Maybe there is a simpler way, and I just haven't worked it out, yet. But either way, could the documentation for the methods package be improved? I can find any obvious info on NULL slots: Introduction Classes Classes_Details setClass slot Again, maybe I missed it. Even setClassUnion, which is what's used in the online examples, doesn't contain a NULL slot example. One more thing: The help file for setClassUnion, uses the term "superclass", incorrectly. Its examples include the following: setClassUnion("maybeNumber", c("numeric", "logical")) If maybeNumber was the superclass of numeric, then every instance of numeric would also be an instance of maybeNumber... ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Abby Spurdle
2020-Sep-24 20:50 UTC
[Rd] Is it possible to simply the use of NULL slots (or at least improve the help files)?
Hi Martin, Thankyou for your response. I suspect that we're not going to agree on the main point. Making it trivially simple (as say Java) to set slots to NULL. So, I'll move on to the other points here. ***Note that cited text uses excerpts only.***> setClassUnion("character_OR_NULL", c("character", "NULL")) > A = setClass("A", slots = c(x = "character_OR_NULL"))I think the above construct needs to be documented much more clearly. i.e. In the introductory and details pages for S4 classes. This is something that many people will want to do. And BasicClasses or NULL-class, are not the most obvious place to start looking, either. Also, I'd recommend the S4 authors, go one step further. Include character_OR_NULL, numeric_OR_NULL, etc, or something similar, in S4's predefined basic classes. Otherwise, contributed packages will (eventually) end up with hundreds of copies of these.> setClassUnion("maybeNumber", c("numeric", "logical")) > every instance of numeric _is_ a maybeNumber, e.g., > > is(1, "maybeNumber") > [1] TRUE> which I think is consistent with the use of 'superclass'Not quite. x <- structure (sqrt (37), class = c ("sqrt.prime", "numeric") ) is (x, "numeric") #TRUE is (x, "maybeNumber") #FALSE So now, an object x, is a numeric but not a maybeNumber. Perhaps a class union should be described as a partial imitation of a superclass, for the purpose of making slots more flexible. B.
Reasonably Related Threads
- Is it possible to simply the use of NULL slots (or at least improve the help files)?
- Is it possible to simply the use of NULL slots (or at least improve the help files)?
- Is it possible to simply the use of NULL slots (or at least improve the help files)?
- setOldClass("xts")
- Subclassing lm