[Rd] Inherited S4 .Data prototype not assigned

Oleg Sklyar osklyar at ebi.ac.uk
Fri May 4 00:37:34 CEST 2007


Martin, thanks for commenting on the issue. I did not know if this was a 
"feature" and therefore did not post before, but:

in EBImage, BioC, the class Image is derived from array and since 
recently (R2.5) I had a problem with the following constructor: 
new("Image", .Data=data). Finally I had to do res <- new("Image"); 
res at .Data <- dat, which worked.

However, what I have problem with, is that R does not have a concept of 
public, private or protected members. Therefore I do not really see why 
.Data should be hidden and why then now and not initially. If this goes 
on this way and there will be no direct access to it, it will simply 
break my whole package.

Best,
Oleg


Martin Morgan wrote:
> 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"     
> 

-- 
Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466



More information about the R-devel mailing list