From murdoch at stats.uwo.ca  Fri Jul  7 14:48:34 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Fri, 07 Jul 2006 08:48:34 -0400
Subject: [Rd] Modifying parsed code
In-Reply-To: <f8e6ff050607070508h7fe3807fu739763f80a472be0@mail.gmail.com>
References: <f8e6ff050607070508h7fe3807fu739763f80a472be0@mail.gmail.com>
Message-ID: <44AE5822.4060605@stats.uwo.ca>

On 7/7/2006 8:08 AM, hadley wickham wrote:
> I would like to take this:
> 
> .img(plot(1:10), filename="a")
> 
> and produce
> 
> plot(1:10)
> 
> ie. whenever .img is used, I want to take the first argument and throw
> away everything else.
> 
> (I am trying to produce a Sweave like environment in which I can apply
> certain functions, but not have them displayed in the output)
> 
> I think I should be able to do it using substitute, but I don't know
> how to operate on language objects.  Any hints would be appreciated.

Supposing that .img has a header something like this:

.img <- function(input, filename)

what you would do is this:

expr <- substitute(input)  # now expr is the unevaluated expression
what <- deparse(expr)     # now what is a text representation of it
eval(expr, envir=parent.frame()) # this should produce the same result
				 # as evaluating input

Duncan Murdoch



[Rd] Modifying parsed code
Duncan Murdoch