Search code examples
rreference-class

how to get list of class names from reference class in R


I have this code:

private$svg <- if(is(private$idaPlotObj, "DivosGridBmiPlot")){
  ...
} else {
  ...
}

in my code and I'm trying to refactor this code and get list of classes from private$idaPlotObj that is reference class, but all I get is this:

[1] "BMIDynamicRatiosPlot"
attr(,"package")
[1] "divosBMI"

when I'm using attr(private$idaPlotObj,"class") or class(private$idaPlotObj)

How can I get all class names from reference class? If I will have 4 classes I will need to check each one with is. I would like to compare vectors to test if class is on the list.


Solution

  • Here is what you could do for reference classes:

     [email protected]$.refClassDef@refSuperClasses
    

    Example:

    setRefClass("Polygon", fields = list(sides="integer"))
    setRefClass("Regular")
    setRefClass("Triangle", contains = "Polygon")
    EQL = setRefClass("EquilateralTriangle", contains = c("Triangle", "Regular"))
    
    tri1 <- EQL$new(sides=3L)
    

    Now to obtain all the classes of tri1 we do:

    [email protected]$.refClassDef@refSuperClasses
    [1] "Triangle"    "Regular"     "Polygon"     "envRefClass"
    

    Edit

    Putting everything together, you could do:

    getRefClassNames <- function(obj) {
       c(class(obj), head([email protected]$.refClassDef@refSuperClasses, -1))
    }