[Rd] S4 method setClass prototype definition question

John Chambers jmc at research.bell-labs.com
Wed Jul 2 15:46:15 MEST 2003


I think you didn't type what you meant to type (see below.)

Sourcing in your class descriptions with 1.7.1, I get what you expected.


> new("Annotation1")
An object of class "Annotation1"
Slot "x":
NULL

Slot "y":
NULL

Slot "catId":
[1] -1

Slot "isResolved":
[1] FALSE

Slot "resolve":
logical(0)

Slot "valid":
[1] TRUE



Matthias Burger wrote:
> 
> 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")

Sorry, naming it an1 still leaves it an object of class "Annotation", as
the printed output tells you.


>  > 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
> 
> ______________________________________________
> R-devel at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc



More information about the R-devel mailing list