[R] Method dispatch in S4

Simon Zehnder szehnder at uni-bonn.de
Sun Aug 4 11:13:52 CEST 2013


So, I found a solution: First in the "initialize" method of class C coerce the C object into a B object. Then call the next method in the list with the B class object. Now, in the "initialize" method of class B the object is a B object and the respective "generateSpec" method is called. Then, in the "initialize" method of C the returned object from "callNextMethod" has to be written to the C class object in .Object. See the code below.

setMethod("initialize", "C", function(.Object, value) {.Object at c <- value; object <- as(.Object, "B"); object <- callNextMethod(object, value); as(.Object, "B") <- object; .Object <- generateSpec(.Object); return(.Object)})

This setting works. I do not know though, if this setting is the "usual" way such things are done in R OOP. Maybe the whole class design is disadvantageous. If anyone detects a mistaken design, I am very thankful to learn.

Best

Simon

 
On Aug 3, 2013, at 9:43 PM, Simon Zehnder <simon.zehnder at googlemail.com> wrote:

> setMethod("initialize", "C", function(.Object, value) {.Object at c <- value; .Object <- callNextMethod(.Object, value); .Object <- generateSpec(.Object); return(.Object)})



More information about the R-help mailing list