1

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.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • I'm not really familiar with Reference classes, therefore a bit a shot in the dark: could you use `myclass <- class(private$idaPlotObj)` to get the class of your object and then use `class_info <- getRefClass(myclass); class_info@generator$def@refSuperClasses` to get the vector of all class names? – starja Dec 22 '20 at 14:25
  • @starja it works. thanks. You can add this as an answer it looks simpler than the answer that was already added. – jcubic Dec 22 '20 at 14:50
  • Done; I think the other answer is a nice one-step solution – starja Dec 22 '20 at 15:17

2 Answers2

2

Here is what you could do for reference classes:

 object@.xData$.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:

tri1@.xData$.refClassDef@refSuperClasses
[1] "Triangle"    "Regular"     "Polygon"     "envRefClass"

Edit

Putting everything together, you could do:

getRefClassNames <- function(obj) {
   c(class(obj), head(obj@.xData$.refClassDef@refSuperClasses, -1))
}
jcubic
  • 61,973
  • 54
  • 229
  • 402
Onyambu
  • 67,392
  • 3
  • 24
  • 53
1

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
starja
  • 9,887
  • 1
  • 13
  • 28