I want to tidy up source code in a way that is compliant with tidy evaluation. Unfortunately, formatR
does not preserve the !!
operator.
formatR::tidy_source(text = "!!little_b", output = FALSE)$text.tidy
## [1] "!(!little_b)"
From Section 7 of Yihui's formatR guide,
In a nutshell, tidy_source(text = code) is basically deparse(parse(text = code))...
But when I call deparse(parse(text = code))
, the text is unusable. Actual behavior:
deparse(parse(text = "1+!!x"))
## [1] "structure(expression(1 + (!(!x))), srcfile = <environment>, wholeSrcref = structure(c(1L, "
## [2] "0L, 2L, 0L, 0L, 0L, 1L, 2L), srcfile = <environment>, class = \"srcref\"))"
The desired result is tidied text:
"1 + !!x
A solution here would probably solve https://github.com/ropensci/drake/issues/200.
To work around this deparser issue you can supply the functional form:
formatR::tidy_source(text = "`!!`(little_b)", output = FALSE)$text.tidy
Note that you need rlang 0.2.0 that will soon be on CRAN.
We've been working on our own deparser that could perhaps be used in formatR in the future, e.g. rlang::expr_deparse()
. We'll also try and see if R core would accept a patch to the base deparser to avoid this unnecessary wrapping in parentheses.
Also check out the styler package that should handle !!
off the bat. It's now the preferred package for formatting R code and it is very configurable.