[R] Can body() return a function's body intact, in order, and as characters ready for editing?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Mar 6 06:46:26 CET 2011


On Sat, Mar 5, 2011 at 8:20 PM, Kingsley G. Morse Jr.
<kingsley at loaner.com> wrote:
> Is my understanding correct that the body()
> function currently can't return a function's body
> intact, in order, and as characters ready for
> editing?
>
> My testing and reading of body()'s help indicate
> that it can not.
>
> Here's what I'm seeing.
>
> Consider pasting
>
>    1+
>
> and a function containing
>
>    x^2
>
> together to get
>
>    1+x^2
>
> As you can see below, body() reports three
> elements, out of order.
>
>    > f<-function(x) x^2; b<-body(f); paste("1+",b, sep="")
>    [1] "1+^" "1+x" "1+2
>
> I realize that this might be worked around with
> something like
>
>    > f<-function(x) x^2; b<-do.call(paste,as.list(c(deparse(body(f)),sep=""))); paste("1+",b, sep="")
>    [1] "1+x^2"
>
> However, I'm asking a different question.
>
> Is my understanding correct that body() can't
> return a function's body intact, in order, and as
> characters ready for editing?
>

body does not return character strings in the first place. It returns
a language object.  You are subsequently turning it into character
strings and the problem occurs there.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list