如何告诉R引用类对象更新其方法定义?

时间:2014-03-18 22:17:31

标签: r reference-class

我正在寻找一种方法来告诉引用类的实例忘记其方法定义之一。例如,我创建了类MyReferenceClass和一个名为my_object的实例我可以调用方法print_hello,一切正常:

MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello")
        }
    )
)
my_object <- MyReferenceClass$new()
my_object$print_hello() # "hello"

如果我通过添加新方法(print_goodbye)来更新类定义,我的现有对象将能够使用它。但是,如果我更改以前定义的方法(print_hello),它将无法更新:

MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello_again")
        },
        print_goodbye = function(){
            print("goodbye")
        }
    )
)
my_object$print_goodbye() # "goodbye" => it works
my_object$print_hello() # "hello" => it doesn't work

有没有办法告诉my_object忘记print_hello的定义?这不起作用:my_object$print_hello <<- NULL

2 个答案:

答案 0 :(得分:3)

AFAIK当试图在“事后”之后通知对象关于类def更改时,即在实例化/创建之后,答案是否定的。

创建S4类的实例后,该对象将“绑定”到类def,就像创建对象时一样。在我看来,这是完全合理的。不确定以前缺失的方法(即print_goodbye())的“成功”更新是否只是“偶然”工作,或者实际上是理想的行为。

推荐更新类defs的方法

我的建议:如果您决定要/需要更新您的课程定义,那么您可以通过重新获取整个项目代码来更安全。通过这种方式,您可以确保在创建实际实例之前之前。我认为其他任何事情都是非常不稳定的黑客攻击。

如果您决定入侵

可能有一些肮脏的方法来破解实际包含类def的Reference Class实例的隐藏.refClassDef对象字段(请参阅my_object$.refClassDef)。但是设置此字段(即在其上使用<-)不起作用:

my_object$.refClassDef <- MyReferenceClass
Error in envRefSetField(x, what, refObjectClass(x), selfEnv, value) : 
  '.refClassDef' is not a field in class "MyReferenceClass"

也没有通过assign()明确分配:

assign(".refClassDef", MyReferenceClass, my_object)
Error in assign(".refClassDef", MyReferenceClass, my_object) : 
  cannot change value of locked binding for '.refClassDef'

更深层次的黑客可能会涉及attributes(my_object$.refClassDef)。 在那里你可能会发现构成ref class def的实际部分。但是,我不知道即使改变任何东西也会“立即”反映出来。

此外,resetClass()可能会为您提供更多见解。


更新时间:2014-03-19

为了处理您的缓存方法,我想到了两种方法:

1。最明显的方法:使用copy()

请参阅?setRefClass

MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello")
        }
    )
)
my_object <- MyReferenceClass$new()
MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello_again")
        },
        print_goodbye = function(){
            print("goodbye")
        }
    )
)

复制前:

my_object$print_hello()
[1] "hello"

复制后:

my_object <- my_object$copy()
my_object$print_hello()
[1] "hello_again"

2。黑客攻击attributes(my_object$.refClassDef)$refMethods(大纲,不工作)

尽管我不建议真正依赖这样的东西,但黑客总是一种很好的方式来深入了解事物的运作方式。

在这种情况下,我们可以尝试修改attributes(my_object$.refClassDef)$refMethods,这是一个包含实际方法defs的环境,因为我猜测这是对象在调用方法时“看起来”的位置。

覆盖实际方法defs没有问题,但似乎没有立竿见影的效果。我猜测还有更多涉及“旧”类def的“链接”,需要以类似的方式手动更新。

请注意,my_object仍然采用打印print_hello的方法"hello"

attributes(my_object$.refClassDef)$refMethods$print_hello
Class method definition for method print_hello()
function () 
{
    print("hello")
}

这就是覆盖函数的样子:

ensureRecentMethods <- function(obj, classname) {
    ## Get generator //
    gen <- getRefClass(classname)

    ## Get names of methods belonging to the class of 'obj' //
    ## This will serve as an index for the update
    idx1 <- names(Filter(function(x) {attr(x, "refClassName") == class(obj)}, 
        as.list(attributes(obj$.refClassDef)$refMethods))
    )
    #idx2 <- names(Filter(function(x) {attr(x, "refClassName")==gen$className}, 
    #    as.list(gen$def@refMethods)
    #))
    ## Note:
    ## 'idx2' could be used to enforce some validity checks such as 
    ## "all old methods must also be present in the updated class def"

    ## Overwrite //
    for (ii in idx1) {
        ## Note how we are overwriting the old method defs in environment
        ## 'attributes(obj$.refClassDef)$refMethods' with the updated 
        ## definitions taken from the generator of the updated class 
        ## 'gen$def@refMethods[[ii]]' by making use of the index retrieved 
        ## one step before ('idx1')
        expr <- substitute(
            assign(x=X, value=VALUE, envir=ENVIR),
            list(
                X=ii,
                VALUE=gen$def@refMethods[[ii]],
                ENVIR=attributes(obj$.refClassDef)$refMethods
            )
        )
        eval(expr)
    }

    ## As at the end of the day ref class objects are nothing more than 
    ## environments, there is no need to explicitly return the actual 
    ## ref class object 'obj' as the original object has already 
    ## been updated (pass-by-reference vs. pass-by-value)
    return(TRUE)
}

应用它:

ensureRecentMethods(obj=my_object, classname="MyReferenceClass")

即使print_hello的def确实被覆盖,该对象仍以某种方式抓住“旧”版本:

attributes(my_object$.refClassDef)$refMethods$print_hello 
## Note the updated method def!

Class method definition for method print_hello()
function () 
{
    print("hello_again")
}

my_object$print_hello()
[1] "hello"

答案 1 :(得分:1)

利用my_class @ generator $ def @ refMethods

如何在原始类中包含update方法,如此处

Manual modifications of the class definition of a Reference Class instance