[Rd] Capturing the expression representing the body of a function

Duncan Murdoch murdoch.duncan at gmail.com
Mon May 2 22:11:21 CEST 2011

On 02/05/2011 3:21 PM, Hadley Wickham wrote:
> Hi all,
> What's the preferred way of capturing the expression representing the
> contents of a function?
> * body(write.csv) gives me a braced expression
> * body(write.csv)[-1] gives me a mangled call
> * as.list(body(write.csv)[-1]) gives me a list of calls
> * as.expression(as.list(body(write.csv)[-1])) is what I want but seems
> like too much work
> Any suggestions?

The body of a function isn't an expression, it's a language object.  A 
language object is represented internally as a pairlist, while an 
expression is represented as a generic vector, i.e. the thing that 
list() gives.

Your 1st try gives you the language object.

The other ones only work when the body consists of a call to `{`, as the 
body of most complex functions does, but not for simple ones like

f <- function(x) 2*x

So I would say your question should be:  "What's the best way to 
construct an expression vector s.t. evaluating its elements in order is 
like evaluating the body of a function?"

And the answer to that is something like

body2expr <- function(f) {
   body <- body(f)
   if (typeof(body) == "language" && identical(body[[1]], quote(`{`))) 
   else as.expression(body)

Duncan Murdoch

More information about the R-devel mailing list