[Rd] Coping with non-standard evaluation in R program analysis
Evan James Patterson
epatters at stanford.edu
Wed Jan 3 01:19:20 CET 2018
Hello R experts,
I plan to develop a tool for dynamic analysis of R programs. I would like to trace function calls at runtime, capturing argument and return values. Following a suggestion made some time ago on this list, my high-level implementation strategy is to rewrite the AST, augmenting call expressions with pre-call and post-call shims to capture the arguments and return value, respectively.
I can think of only one fundamental conceptual obstacle to this approach: R functions are not necessarily referentially transparent. The arguments received by a function are not values but promises. They can be evaluated directly ("standard evaluation"), after applying arbitrary syntactic transformations ("non-standard evaluation", aka NSE), or not at all. Therefore, if you peek at the values of function arguments before evaluating the function, you risk altering the semantics of the program, possibly fatally.
I'm looking for general advice about how to cope with NSE in this context. I also have some specific questions:
1) Is it possible to determine whether a given function (primitive, in R, or external) uses NSE on some or all of its arguments?
2) Is it possible to inspect the promise objects received by functions, say to determine whether they have been evaluated, without actually evaluating them? The R manual is not encouraging in this area:
[[alternative HTML version deleted]]
More information about the R-devel