From Ma.Binder at campus.lmu.de  Fri Jul  7 23:45:43 2017
From: Ma.Binder at campus.lmu.de (Binder, Martin)
Date: Fri, 07 Jul 2017 17:45:43 -0400
Subject: [Rd] bug: deparse sometimes omits parentheses for unary operators
Message-ID: <3bfc58402caae8fd20f3a263405f2506@campus.lmu.de>

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



[Rd] bug: deparse sometimes omits parentheses for unary operators
Binder, Martin