From murdoch.duncan at gmail.com  Wed May  4 15:07:36 2011
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Wed, 04 May 2011 09:07:36 -0400
Subject: [Rd] Capturing the complete unevaluated expression
 corresponding to function (body + formals)
In-Reply-To: <BANLkTikYQ-E44a1bD3zVi75Y0cAfRooSsQ@mail.gmail.com>
References: <BANLkTikYQ-E44a1bD3zVi75Y0cAfRooSsQ@mail.gmail.com>
Message-ID: <4DC14F98.7090303@gmail.com>

On 04/05/2011 8:38 AM, Hadley Wickham wrote:
> Hi all,
>
> Is there any way to capture the complete unevaluated expression
> corresponding to a function?  I want the equivalent of
>
> x<- quote(function(x) x = 3)
>
> But captured after the function is created.  Body and formals each
> captures a part, but is there a built in way to get the whole thing?

Internally, "function" is a function that produces a closure object.  
After you call it and the function is created, the inputs are gone, all 
you have is the result.  But you can reconstruct it as you did below, 
because "function" basically just glues the parts together (and adds an 
environment as well).

Duncan Murdoch
> f<- function(x) x = 3
> y<- call("function", formals(f), body(f), attr(f, "source"))
> identical(x, y)
>
> Hadley
>



[Rd] Capturing the complete unevaluated expression corresponding to function (body + formals)
Duncan Murdoch