R测试参考类

时间:2012-11-08 21:09:23

标签: r reference-class

是否有一种快速而肮脏的方法来测试实例是否来自引用类?

标准R对象测试产生以下结果 - 但似乎没有任何东西专门标记引用类。

classy <- setRefClass('classy',
    fields = list(
        count = 'numeric'
    ),
    methods = list(
        initialize = function( data=NULL ) {
            .self$data <<- data
        }
    )
)

instance <- classy$new() # instantiation

isS4(instance) # TRUE
mode(instance) # "S4"
typeof(instance) # "S4"
class(instance) # [1] "classy" attr(,"package") [1] ".GlobalEnv"
dput(instance) # new("classy", .xData = <environment>)
str(instance) # 
# Reference class 'classy' [package ".GlobalEnv"] with 1 fields
#  $ count: num(0) 
#  and 13 methods, of which 1 are possibly relevant:
#    initialize

1 个答案:

答案 0 :(得分:6)

试试这个:

 inherits(instance, "envRefClass")
# should return [1] TRUE

这可以在help(ReferenceClasses)的“继承”部分找到。我怀疑John Chambers可能会反对称这个“脏”。

Apropos Hadley的评论,is被记录为与inherits大致相同,但具有识别条件继承的附加能力:

is(instance, "envRefClass")
#TRUE