This problem bothered me for months now. Today I found a very handy solution inspired by https://stat.ethz.ch/pipermail/r-help/2012-January/299743.html
Let's start with a ReferenceClass which 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()
a b
1 1 0.1349983
The implementation of getSecondRow
does obviously not provide the desired result. So the fixed method should actually look like
getSecondRow = function(){
return( xy[2,] )
}
Classes without explicit implementation of the constructor
The trick instead of loading the class and reproducing the object from scratch is to construct an new object from the existing, using the copy constructor functionality of the default initialize( ... )
constructor. After debugging and reloading the class you can just copy the existing object with the implementation into the same variable by
# NOTRUN
mc <- MyClass$new( mc )
Classes with overwritten constructor.
However, in the case presented here, the standard constructor is already overwritten. But in such cases you can simply use the functionality of callSuper( ... )
such that your constructor should look like
initialize = function( df, ... ){
callSuper( ... )
if( !missing( df ) ){
xy <<- df
}
}
Finally, your fixed object is derived by
mc <- MyClass$new( mc$xy, mc )
mc$getSecondRow()
a b
2 2 0.8452587