Getting a list of subclasses of a reference class

66 Views Asked by At

I created an AbstractFoo reference class and several subclasses, now I want to get all the names of the subclasses (AFoo, BFoo, CFoo). It seems this is possible using the subclasses slot of an S4 class, but I'd like to get the same sort of thing for a reference class.

For instance my code might be:

AbstractFoo <- setRefClass("AbstractFoo")
AFoo <- setRefClass("AFoo", contains = c("AbstractFoo"))
BFoo <- setRefClass("BFoo", contains = c("AbstractFoo"))
CFoo <- setRefClass("CFoo", contains = c("AbstractFoo"))

So I would want something that returns c("AFoo", "BFoo", "CFoo").

1

There are 1 best solutions below

0
d.b On BEST ANSWER
foo = function(nm){
    objs = ls(envir = .GlobalEnv)
    tmp = setNames(lapply(objs, function(objName){
        thisObj = get(objName)
        if (class(thisObj) == "refObjectGenerator" & objName != nm){
            is(thisObj(), nm)
        }
    }), objs)
    tmp = unlist(tmp)
    names(tmp)[tmp]
}

foo("AbstractFoo")
#[1] "AFoo" "BFoo" "CFoo"