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(){
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