How to define an S4 prototype for inherited slots

oop, r, r-s4

Solution

You just need to name the argument:

setClass("A", representation(a="character"))
setClass("B", contains="A", prototype=prototype(a="hello"))

Problem

I have a base class (let's call it "A") whose representation is common to many other classes. Therefore I define other classes, such as "B", to contain this class. I would like to set the prototype of these other classes (B) to include the default values for the slots inherited from A. I thought this would be natural: ``` setClass("A", representation(a="character")) setClass("B", contains="A", prototype(a = "hello")) ``` But it produces the error: ``` Error in representation[!slots] : object of type 'S4' is not subsettable ``` Not sure why this happens. If I omit the prototype I can do: ``` setClass("B", contains="A") ``` and then hack my own generator function: ``` new_B <- function(...){ obj <- new("B", ...) obj@a = "hello" obj } ``` and then create my object based on the prototype with `new_B()`, but that's terribly crude and ugly compared to using the generic generator `new("B")` and having my prototype...

Original source

Related problems