[Rd] Inherited S4 .Data prototype not assigned

Martin Morgan mtmorgan at fhcrc.org
Thu May 3 21:56:06 CEST 2007


An S4 class inheriting from another class with a prototype does not
inherit (at least sometimes) the prototype for .Data:

> setClass("A", "numeric", 1)
> setClass("B", "A")
> new("A")
An object of class "A"
[1] 1
> new("B")
An object of class "B"
numeric(0)

My expectation is that prototype inheritance of .Data behaves as for
regular slots:

> setClass("A",
+          representation=representation(x="numeric"),
+          prototype=prototype(x=1))
[1] "A"
> setClass("B", "A")
[1] "B"
> new("B")
An object of class "B"
Slot "x":
[1] 1

I think this traces first to an errant ! that claims the data part is
filled, when it is not yet, and second to a call to getDataPart with a
primitive that occupies, but does not have, a .Data slot.

Martin

--- src/library/methods/R/RClassUtils.R (revision 41426)
+++ src/library/methods/R/RClassUtils.R (working copy)
@@ -45,7 +45,7 @@
     ## try for a single superclass that is not virtual
     supers <- names(extends)
     virtual <- NA
-    dataPartDone <- length(slots)==0 || !is.na(match(".Data", snames))
+    dataPartDone <- length(slots)==0 || is.na(match(".Data", snames))
     dataPartClass <- if(dataPartDone) "ANY" else elNamed(slots, ".Data")
     prototype <- ClassDef at prototype
     ## check for a formal prototype object (TODO:  sometime ensure that this happens
@@ -79,8 +79,15 @@
             else if(length(slots) > 0) {
                 for(slotName in slotsi) {
                     if(identical(slotName, ".Data")) {
-                        if(!dataPartDone) {
-                            prototype <- setDataPart(prototype, getDataPart(pri))
+                        if (!dataPartDone) {
+                            temp <- getClass(class(pri))@slots
+                            if (length(temp)==0 ||
+                                is.na(match(".Data", names(temp)))) {
+                                prototype <- pri
+                                attributes(prototype) <- NULL
+                            } else
+                                prototype <-
+                                    setDataPart(prototype, getDataPart(pri))
                             dataPartDone <- TRUE
                         }
                     }
> sessionInfo()
R version 2.6.0 Under development (unstable) (2007-05-03 r41426) 
x86_64-unknown-linux-gnu 

locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C

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

-- 
Martin Morgan
Bioconductor / Computational Biology
http://bioconductor.org



More information about the R-devel mailing list