[Rd] S4 method setClass prototype definition question

Matthias Burger ml-it-r-devel at epigenomics.com
Tue Jun 24 18:18:57 MEST 2003


Dear list,

this is not a problem report -- I would like to ask for advise what the 
recommended and safe way to perform the following is or what problems might 
arise in doing it differently.

The question is:  What is the recommended way of providing a default value in a 
prototype statement for a derived class for a slot provided from a parent class?
I have first consulted the methods package reference but am still unclear to as 
this point.


The following is a bit length example + variations thereof taken from a real 
project.

I would appreciate any comments and insights shared.

Best regards,

   Matthias


##
##  R 1.7.1 on Debian Linux (compiled with gcc 3.2.2)
 > version
          _
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    1
minor    7.1
year     2003
month    06
day      16
language R

##  define a base class (virtual) without prototype
   setClass("IDataType",
            representation("VIRTUAL",
                           valid="logical"),
            validity=NULL,
            sealed=TRUE)

##  same now with prototype definition
   setClass("IDataType1",
            representation("VIRTUAL",
                           valid="logical"),
            prototype=list(valid=TRUE),
            validity=NULL,
            sealed=TRUE)


## and now the derived class in question
## the slot under investigatin is 'valid'
    setClass("Annotation",
             representation ("IDataType",
                             x="vector",
                             y="vector",
                             catId="integer",
                             isResolved="logical",
                             resolve="logical"),
             prototype(isResolved=FALSE,
                       catId=as.integer(-1)),
             validity=NULL,
             sealed=TRUE)

##  instantiate an object an
an<-new("Annotation")
 > an
An object of class "Annotation"
Slot "x":
NULL

Slot "y":
NULL

Slot "catId":
[1] -1

Slot "isResolved":
[1] FALSE

Slot "resolve":
logical(0)

Slot "valid":
logical(0)

##  which is what I expected. Now in order to set a default for
##  the 'valid' slot (provided by the parent class 'IDataType') in the
##  Annotation class I tried

setClass("Annotation1",
             representation ("IDataType",
                             x="vector",
                             y="vector",
                             catId="integer",
                             isResolved="logical",
                            resolve="logical"),
             prototype(isResolved=FALSE,
                       catId=as.integer(-1),
                       valid=TRUE),
             validity=NULL,
             sealed=TRUE)

##  instantiate an object an1
 > an1<-new("Annotation")
 > an1
An object of class "Annotation"
Slot "x":
NULL

Slot "y":
NULL

Slot "catId":
[1] -1

Slot "isResolved":
[1] FALSE

Slot "resolve":
logical(0)

Slot "valid":
logical(0)

##  now this is NOT what I had expected; my prediction would have been to see
##  slot 'valid' set to TRUE

##  ok, so this is an alternative to get what I aimed at, i.e. use the 2nd
##  virtual base class IDataType1 with prototype definition
  setClass("Annotation2",
             representation ("IDataType1",
                             x="vector",
                             y="vector",
                             catId="integer",
                             isResolved="logical",
                             resolve="logical"),
             prototype(isResolved=FALSE,
                       catId=as.integer(-1)),
             validity=NULL,
             sealed=TRUE)
[1] "Annotation2"
 > an2<-new("Annotation2")
 > an2
An object of class "Annotation2"
Slot "x":
NULL

Slot "y":
NULL

Slot "catId":
[1] -1

Slot "isResolved":
[1] FALSE

Slot "resolve":
logical(0)

Slot "valid":
[1] TRUE

##  so this does what I aimed at but surprises me as I thought it would
##  not make sense
##  to provide a prototype for a virtual class

## reading further in the methods package reference manual
## (refman.pdf: The Methods Package)
## I conclude that setAs

##  use IDataType class w prototype and explicit contains statement
 >  setClass("Annotation3",
            representation ("IDataType1",
                            x="vector",
                            y="vector",
                            catId="integer",
                            isResolved="logical",
                            resolve="logical"),
            prototype(isResolved=FALSE,
                      catId=as.integer(-1)),
            contains=as.character("IDataType1"),
            validity=NULL,
            sealed=TRUE)

[1] "Annotation3"
 > an3<-new("Annotation3")
 > an3
An object of class "Annotation3"
Slot "x":
NULL

Slot "y":
NULL

Slot "catId":
[1] -1

Slot "isResolved":
[1] FALSE

Slot "resolve":
logical(0)

Slot "valid":
[1] TRUE

##  so this works aswell
##  and yet another alternative:
##  use IDataType class w/o prototype but explicitly use
##  contain=as.character("IDataType")
##  statement and set slot 'valid' prototype
 >   setClass("Annotation4",
             representation ("IDataType",
                             x="vector",
                             y="vector",
                             catId="integer",
                             isResolved="logical",
                             resolve="logical"),
             prototype(isResolved=FALSE,
                       catId=as.integer(-1),
                       valid=TRUE),
             contains=as.character("IDataType"),
             validity=NULL,
             sealed=TRUE)

[1] "Annotation4"

 > an4<-new("Annotation4")
 > an4
An object of class "Annotation4"
Slot "x":
NULL

Slot "y":
NULL

Slot "catId":
[1] -1

Slot "isResolved":
[1] FALSE

Slot "resolve":
logical(0)

Slot "valid":
[1] TRUE

##  This also works as expected.

##  So what is the recommended way of providing a default in a prototype
##  statement for a
##  slot provided from a parent class?




-- 
Matthias Burger

Bioinformatics R&D
Epigenomics AG                      www.epigenomics.com
Kleine Pr?sidentenstra?e 1          fax:   +49-30-24345-555
10178 Berlin Germany                phone: +49-30-24345-0



More information about the R-devel mailing list