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

Duncan Murdoch murdoch.duncan at gmail.com
Wed May 4 15:07:36 CEST 2011


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
>



More information about the R-devel mailing list