When turning a language object into a string (e.g. using dput, deparse, or just printing it), R usually inserts parentheses so that the printed string is an
accurate representation of the underlying AST.

Example (good):
(expr = substitute(a * 10, list(a = quote(if (TRUE) 1 else 0))))
(if (TRUE) 1 else 0) * 10
eval(expr)
[1] 10

The representation is incorrect, however, when unary operators around language
constructs like "if", "for" etc. are involved.

Example (bad):
(expr = substitute(-a * 10, list(a = quote(if (TRUE) 1 else 0))))
-if (TRUE) 1 else 0 * 10

The parentheses around the "if"-construct are missing. The expected output
(the true representation of the expression) is, in fact:

-(if (TRUE) 1 else 0) * 10

as can be tested when evaluating it (which shows that this is not a bug in
"substitute"):

eval(expr)
[1] -10

The deparsed string on the other hand evaluates to:

-if (TRUE) 1 else 0 * 10
[1] -1

Even when using deparse with control="all", which comes, per its help file, "closest to making 'deparse()' an inverse of 'parse()'", this bug persists:

deparse(expr, control="all")
[1] "quote(-if (TRUE) 1 else 0 * 10)"

Only some language constructs appear to be affected. Unary operators applied to function calls, other unary operators, or binary operators render as expected:

(expr = substitute(-a * 10, list(a = quote(!0))))
-(!0) * 10

I am using the R version 3.4.0 (2017-04-21) platform x86_64-redhat-linux-gnu
package from Fedora 25.

Regards,

Martin Binder

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to