Modify functions in R using body, formals and environment methods : <p><strong>function definition</strong></p>

<pre><code>f &lt;- function(x) {
   x + x*x
}
</code></pre>

<p>3 methods i.e. <code>body</code>, <code>formals</code> and <code>environment</code> can be used for modification</p>

<p><strong>body</strong></p>

<pre><code>&gt; body(f)
{
    x + x * x
}
</code></pre>

<p>If we want to change the body using <code>body</code></p>

<pre><code>&gt; body(f) &lt;- expression({x*x*x})
&gt; f
function (x) 
{
    x * x * x
}
</code></pre>

<p>see its changed.</p>

<p><strong>formals</strong></p>

<p>If want to change the arguments using <code>formals</code> to <code>(x = 3, y = 6)</code></p>

<pre><code>&gt; formals(f) &lt;- list(x = 3, y = 4)
&gt; f
function (x = 3, y = 4) 
{
    x * x * x
}
</code></pre>

<p>see its changed.</p>

<p>But if want to change the arguments to <code>(x, y)</code> instead. Obviously <code>formals(f) &lt;- list(x, y)</code> will not work.</p>

<p>Any help will be appreciated.</p>
