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. Thanks, Hadley
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
Assuming the desired output is a character string try:> f <- function(x) deparse(substitute(x)) > f(3+y)[1] "3 + y" On 7/7/06, hadley wickham <h.wickham at gmail.com> 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. > > Thanks, > > Hadley > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
> I would like to take this: > > .img(plot(1:10), filename="a") > > and produce > > plot(1:10) >Peter Dalgaard provided me with this: f <- function(e) { if (!is.recursive(e)) e else if (e[[1]] == quote(.img)) e[[2]] else as.call(lapply(e, f)) }> f(quote({a<-1;.img(abc,123)})){ a <- 1 abc } which is exactly what I was looking for. Thanks Peter! Hadley
On Fri, 7 Jul 2006, hadley wickham wrote:> I would like to take this: > > .img(plot(1:10), filename="a")I presume from your title that you want to take foo <- parse(text='.img(plot(1:10), filename="a")') ? That's an expression. In which case> foo[[1]][[2]]plot(1:10) (first expression which a call, and calls are symbol, arg1, arg2 ...).> > 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. > > Thanks, > > Hadley > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595