R, R6, Get Full Class Name from R6Generator Object

丶灬走出姿态 提交于 2019-12-06 09:23:21

There isn't a built-in way to find all the classes that B inherits without calling B$new(). This is because inheritance is determined when objects are instantiated, not when the classes are created. A class (AKA generator object) knows the name of the class it inherits from, but that name is evaluated only when objects are instantiated.

You could do something like this to find the inheritance chain, but this uses some internal APIs that could possibly change in the future (although they probably won't):

findClasses <- function(x) {
  if (is.null(x))
    return(NULL)
  parent <- x$get_inherit()
  c(x$classname, findClasses(parent))
}

A <- R6::R6Class("Base",NULL)
B <- R6::R6Class("Middle", inherit = A)
C <- R6::R6Class("Top", inherit = B)
findClasses(C)
#> [1] "Top"    "Middle" "Base"  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!