[Rd] How to test if an object/argument is "parse tree" - without evaluating it?

peter dalgaard pdalgd at gmail.com
Thu May 1 23:44:27 CEST 2014


My take would be that this is barking up the wrong tree. If you want to pass expressions in a way that a function can recognize, use formulas or expression objects. 

One problem is that pretty much every unevaluated argument is a "parse tree". The only other thing it can be is a constant object, but that is really just the simplest possible parse tree.  

In what situation exactly were you expecting isParseTree to return FALSE?

-pd


On 01 May 2014, at 22:39 , Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:

> This may have been asked before, but is there an elegant way to check
> whether an variable/argument passed to a function is a "parse tree"
> for an (unevaluated) expression or not, *without* evaluating it if
> not?
> 
> Currently, I do various rather ad hoc eval()+substitute() tricks for
> this that most likely only work under certain circumstances. Ideally,
> I'm looking for a isParseTree() function such that I can call:
> 
> expr0 <- foo({ x <- 1 })
> expr1 <- foo(expr0)
> stopifnot(identical(expr1, expr0))
> 
> where foo() is:
> 
> foo <- function(expr) {
> if (!isParseTree(expr))
>   expr <- substitute(expr)
> expr
> }
> 
> I also want to be able to do:
> 
> expr2 <- foo(foo(foo(foo(expr0))))
> stopifnot(identical(expr2, expr0))
> 
> and calling foo() from within other functions that may use the same
> "tricks".  The alternative is of course to do:
> 
> foo <- function(expr, substitute=TRUE) {
> if (substitute) expr <- substitute(expr)
> expr
> }
> 
> but it would be neat to do this without passing an extra argument.  If
> this is not possible to implement in plain R, can this be done
> internally inspecting SEXP:s and so on?  Even better would be if
> substitute() could do this for me, e.g.
> 
> expr <- substitute(expr, unlessAlreadyDone=TRUE)
> 
> Any suggestions?
> 
> Thanks,
> 
> Henrik
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-devel mailing list