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

257 Views Asked by At

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.

2

There are 2 best solutions below

0
Onyambu On BEST ANSWER

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))
}
0
starja On

Here is a solution that splits it up into several steps:

  1. get the current class name of the object:
myclass <- class(private$idaPlotObj)
  1. use getRefClass to get the information about the associated classes:
class_info <- getRefClass(myclass)
  1. you get an object back where the info is a bit hidden, so you have to extract it:
class_info@generator$def@refSuperClasses