[Rd] Unexpected failure when calling new() with unnamed arg and
Hervé Pagès
hpages at fredhutch.org
Wed May 13 00:18:42 CEST 2015
Hi,
The man page for new() suggests that if 'a' is an object with slots
"slot1" and "slot2" and C is a class that extends the class of 'a',
then the 2 following calls should be equivalent:
new("C", a, ...)
new("C", slot1=a at slot1, slot2=a at slot2, ...)
This is generally the case but I just ran into a situation where it's
not. In the following example the former fails while the latter works:
setClass("A", representation(slot1="numeric", slot2="logical"))
setClass("B", contains="A", representation(design="formula"))
setClass("C", contains="B")
a <- new("A", slot1=77, slot2=TRUE)
new("C", a, design=x ~ y) # fails
new("C", slot1=a at slot1, slot2=a at slot2, design=x ~ y) # works
Note that new("B", a, design=x ~ y) works so the 3-level class
hierarchy is really needed in order to reproduce.
Probably related to this, I also noted that new("B") and/or new("C")
return invalid objects:
c <- new("C")
validObject(c)
# Error in validObject(c) :
# invalid class “C” object: invalid object for slot "design"
# in class "C": got class "S4", should be or extend class "formula"
is(c at design, "formula")
# [1] FALSE
class(c at design)
# [1] "S4"
Note that 'c' can be fixed:
c at design <- formula(NULL)
validObject(c)
# [1] TRUE
Maybe something that the default "initialize" method should take care
of?
Another singularity that is maybe at the root of all of this is that
the "formula" S4 class is virtual:
showClass("formula")
# Virtual Class "formula" [package "methods"]
#
# Slots:
#
# Name: .S3Class
# Class: character
#
# Extends: "oldClass"
so a bare call to new("formula") fails:
new("formula")
# Error in new("formula") :
# trying to generate an object from a virtual class ("formula")
Shouldn't new("formula") just return an "empty" S3 formula (like
formula(NULL) does), in the same way that new("integer") returns
an empty ordinary integer vector?
Thanks,
H.
> sessionInfo()
R version 3.2.0 Patched (2015-04-17 r68202)
Platform: x86_64-unknown-linux-gnu (64-bit)
Running under: Ubuntu 14.04.2 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
--
Hervé Pagès
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024
E-mail: hpages at fredhutch.org
Phone: (206) 667-5791
Fax: (206) 667-1319
More information about the R-devel
mailing list