Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This problem bothered me for months now. Today I found a very handy solution inspired by <a href="https://stat.ethz.ch/pipermail/r-help/2012-January/299743.html" rel="nofollow">https://stat.ethz.ch/pipermail/r-help/2012-January/299743.html</a></p> <p>Let's start with a ReferenceClass which has an erroneous implementation:</p> <pre><code>MyClass &lt;- setRefClass( "MyClass", fields = list( xy = "data.frame" ), methods = list( initialize = function( df ){ if( !missing( df ) ){ xy &lt;&lt;- df } }, getSecondRow = function(){ # A mistake happend here return( xy[1,] ) } ) ) mc &lt;- MyClass$new( data.frame( a = 1:10, b = rnorm(10) ) ) mc$getSecondRow() a b 1 1 0.1349983 </code></pre> <p>The implementation of <code>getSecondRow</code> does obviously not provide the desired result. So the fixed method should actually look like</p> <pre><code> getSecondRow = function(){ return( xy[2,] ) } </code></pre> <h2>Classes without explicit implementation of the constructor</h2> <p>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 <code>initialize( ... )</code> constructor. After debugging and reloading the class you can just copy the existing object with the implementation into the same variable by</p> <pre><code># NOTRUN mc &lt;- MyClass$new( mc ) </code></pre> <h2>Classes with overwritten constructor.</h2> <p>However, in the case presented here, the standard constructor is already overwritten. But in such cases you can simply use the functionality of <code>callSuper( ... )</code> such that your constructor should look like</p> <pre><code> initialize = function( df, ... ){ callSuper( ... ) if( !missing( df ) ){ xy &lt;&lt;- df } } </code></pre> <p>Finally, your fixed object is derived by</p> <pre><code>mc &lt;- MyClass$new( mc$xy, mc ) mc$getSecondRow() a b 2 2 0.8452587 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload