[Rd] deparse with parentheses for SUBSET
Lukas Stadler
lukas.stadler at oracle.com
Mon Jan 4 17:09:31 CET 2016
Hi,
maybe there’s a reason for it, but the discrepancy between the handling of `[` and `$` in deparsing seems odd to me:
> substitute(a[1], list(a = quote(x * y)))
x * y[1]
> substitute(a$b, list(a = quote(x * y)))
(x * y)$b
The former is still executed in the right order (`*` first, then `[`), which is not what you’d expect looking at the deparse result.
Some code that shows the execution order:
x <- 1; y <- 1; class(y) <- "foo"; `*.foo` <- function(...) -1; expr <- substitute(a[1], list(a=quote(x * y))); list(expr=expr, eval=eval(expr), "x * y[1]"=x * y[1], "(x * y)[1]"=(x * y)[1])
The following simple fix solves the problem for me:
diff -u -r A/src/main/deparse.c B/src/main/deparse.c
--- R-devel 2/src/main/deparse.c 2015-08-09 18:09:04.000000000 +0200
+++ R-devel/src/main/deparse.c 2016-01-04 16:15:09.000000000 +0100
@@ -971,7 +971,11 @@
print2buff(")", d);
break;
case PP_SUBSET:
+ if ((parens = needsparens(fop, CAR(s), 1)))
+ print2buff("(", d);
deparse2buff(CAR(s), d);
+ if (parens)
+ print2buff(")", d);
if (PRIMVAL(SYMVALUE(op)) == 1)
print2buff("[", d);
else
With this applied, the output is more consistent:
> substitute(a[1], list(a = quote(x * y)))
(x * y)[1]
> substitute(a$b, list(a = quote(x * y)))
(x * y)$b
Best Regards,
Lukas Stadler
More information about the R-devel
mailing list