Skip to content
Prev 53831 / 63421 Next

bug: deparse sometimes omits parentheses for unary operators

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):
(if (TRUE) 1 else 0) * 10
[1] 10

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

Example (bad):
-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"):
[1] -10

The deparsed string on the other hand evaluates to:
[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:
[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:
-(!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