From wdunlap at tibco.com  Thu May  1 23:13:53 2014
From: wdunlap at tibco.com (William Dunlap)
Date: Thu, 1 May 2014 14:13:53 -0700
Subject: [Rd] How to test if an object/argument is "parse tree" -
 without evaluating it?
In-Reply-To: <CAFDcVCRpqqPkhFhaNpqe3ye-jo-8sWOODShKHneonTSB7m5Uvg@mail.gmail.com>
References: <CAFDcVCRpqqPkhFhaNpqe3ye-jo-8sWOODShKHneonTSB7m5Uvg@mail.gmail.com>
Message-ID: <CAF8bMcZU+9hab-TPHPYC_mweNvBOyitR26uxf5gso79oBV6z=A@mail.gmail.com>

> 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?

I doubt it.

Some packages say that if the argument is a formula then its right
hand side will be used, unevaluated.  (You could issue a warning if
the formula had a left side.)  This offloads the logic to the ~ or
formula function.  It also has the advantage that environment(formula)
tells you where the symbols in the expression should be looked up.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, May 1, 2014 at 1:39 PM, 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



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