Update reference class method

I am currently developing Reference (R5) classes for large objects that take a while to create, and I wonder if anyone knows a better way to develop methods than redefine the class using setRefClass and play the object each time the method is updated.

In other words: is it possible to override the methods of an existing object of class Reference?

+4
source share
2 answers

I thought about doing the following:

test <- setRefClass("TEST", fields = list(a = "numeric"), methods = list( funA = function(){ a <<- a+1 }, initialize = function(){ a <<- 1 callSuper() } ) ) ex1 <- test$new() ex1$funA() test$methods(funA = function(){ a <<- a+10 } ) ex$funA # Class method definition for method funA() # function () # { # a <<- a + 1 # } # <environment: 0x0537f8ac> ex1 <- test$new()$import(ex1) ex$funA # Class method definition for method funA() # function () # { # a <<- a + 10 # } # <environment: 0x04badc5c> 

I believe that you can overwrite the previous method in the class using class$methods( , then you can set a new object from an overridden class and import the old object. I would like to note:

"All methods for a class must be defined in the source code, which defines the class, usually as part of a package. In particular, methods cannot be overridden in a class in an attached package with a namespace: the class method checks the binding of the class definition binding." from ?setRefClass .

+1
source

This problem has been bothering me for several months. Today I found a very convenient solution inspired by https://stat.ethz.ch/pipermail/r-help/2012-January/299743.html

Let's start with a ReferenceClass that has an erroneous implementation:

 MyClass <- setRefClass( "MyClass", fields = list( xy = "data.frame" ), methods = list( initialize = function( df ){ if( !missing( df ) ){ xy <<- df } }, getSecondRow = function(){ # A mistake happend here return( xy[1,] ) } ) ) mc <- MyClass$new( data.frame( a = 1:10, b = rnorm(10) ) ) mc$getSecondRow() ab 1 1 0.1349983 

The implementation of getSecondRow , obviously, does not give the desired result. So the fixed method should look like

  getSecondRow = function(){ return( xy[2,] ) } 

Classes without an explicit constructor implementation

The trick instead of loading the class and playing the object from scratch is to build a new object from the existing one, using the functionality of the default constructor copy constructor initialize( ... ) . After debugging and reloading the class, you can simply copy the existing object with the implementation into the same variable

 # NOTRUN mc <- MyClass$new( mc ) 

Classes with rewritten constructor.

However, in the case presented here, the standard constructor is already overwritten. But in such cases, you can simply use the callSuper( ... ) functionality to make your constructor look like

  initialize = function( df, ... ){ callSuper( ... ) if( !missing( df ) ){ xy <<- df } } 

Finally, your fixed object is displayed

 mc <- MyClass$new( mc$xy, mc ) mc$getSecondRow() ab 2 2 0.8452587 
+1
source

Source: https://habr.com/ru/post/1379969/


All Articles