以与整洁评估兼容的方式整理文本

Tidying text in a way compatible with tidy evaluation

我想以符合 tidy evaluation 的方式整理源代码。不幸的是,formatR 不保留 !! 运算符。

formatR::tidy_source(text = "!!little_b", output = FALSE)$text.tidy
## [1] "!(!little_b)"

来自 Yihui's formatR guide 的第 7 节,

In a nutshell, tidy_source(text = code) is basically deparse(parse(text = code))...

但是当我调用deparse(parse(text = code))时,文本无法使用。实际行为:

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\"))" 

想要的结果是整理后的文字:

"1 + !!x

此处的解决方案可能会解决 https://github.com/ropensci/drake/issues/200

要解决此解析器问题,您可以提供函数形式:

formatR::tidy_source(text = "`!!`(little_b)", output = FALSE)$text.tidy

请注意,您需要 rlang 0.2.0,它很快就会出现在 CRAN 上。

我们一直在研究自己的解析器,将来可能会在 formatR 中使用,例如rlang::expr_deparse()。我们还将尝试查看 R 核心是否会接受基础解析器的补丁,以避免这种不必要的括号包装。

另请检查应立即处理 !!styler 包。它现在是格式化 R 代码的首选包,而且非常可配置。