R语言意识的代码重新格式化/重构工具? [英] R language aware code reformatting/refactoring tools?

查看:132
本文介绍了R语言意识的代码重新格式化/重构工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我发现自己正在使用遍及整个地图的R代码,其编码风格-多位作者和个别作者并不严格遵循单一的结构。我希望某些任务比目前要自动化的更好。

Recently I've found myself working with R code that is all over the map in terms of coding style - multiple authors and individual authors who aren't rigorous about sticking to a single structure. There are certain tasks that I'd like to automate better than I currently do.

我正在寻找一种可以管理以下任务的工具(已列出)以欲望的增加顺序,但也以对生存的怀疑顺序增加。

I'm looking for a tool (or tools) that manage the following tasks - listed in increasing order of desire but also somewhat in increasing order of skepticism of existence.


  • 基本格式。诸如将 if(foo)转换为 if(foo)并在支撑位置和诸如此类的事情上实现统一。

  • Basic formatting. Things like converting "if( foo )" to "if (foo)" and achieving uniformity in terms of brace location and that sort of thing.

转换 foo $ blah到 foo [[ blah]]进行列表访问。理想情况下,它至少能够猜测一个对象是否真的是列表而不是data.frame,而仅转换列表。

Converting "foo$blah" to "foo[["blah"]]" for list access. Ideally it'd be able to at least make a guess if an object was really a list and not a data.frame and only convert lists.

转换 ='到'<-'。是的,这是一个简单的搜索和替换操作-并非如此。该工具(或regexp)需要具有语言意识,以便能够转换 x = 5但不能转换 foo(x = 5)。不仅可以简单地替换符号,而且还可以确保赋值运算符两侧都留有一个空白,这也非常好。

Converting '=' to '<-'. Yes, this is a simple search and replace - but not really. The tool (or regexp) needs to be language aware such that it knows to convert "x = 5" but not "foo(x=5)". It'd also be really nice to not simply replace the symbol but also to ensure a single whitespace on both sides of the assignment operator.

变量重命名,尤其是跨职能和文件。例如,假设一个列表包含一个元素 foo,那么我希望能够一次将其更改为 foobar,而不必在整个代码流中跟踪该列表的每次使用。我想这将要求该工具具有整个控制流程,以便识别诸如列表之类的东西,该列表作为另一个名称存在于另一个函数中。

Variable renaming, particularly across functions & files. For instance, suppose a list has an element "foo", I'd love to be able to change it to "foobar" once and not have to track down every usage of that list throughout the entire code flow. I'd imagine this would require the tool to be able the entire flow of control in order to identify things such as that list existing as another name in a different function.

命名约定。我希望能够定义一些标准的命名约定(例如 Google的或其他内容),并确定所有功能,变量等并将其转换。请注意,这与上一个条目有关,例如列表元素。

Naming conventions. I'd love to be able to define some standard naming convention (e.g. Google's or whatever) and have it identify all of the functions, variables, etc and convert them. Note that this ties in with the previous entry for things like list elements.

随意列出基本的unix处理命令(例如sed),只要它确实足够聪明,至少通常不会搞砸(例如,将 foo(x = 5)转换为 foo(x< -5))。

Feel free to list basic unix processing commands (e.g. sed) as long as it'll really be smart enough to at least usually not screw things up (e.g. converting "foo(x=5)" to "foo(x<-5)").

我猜想,如果这样的工具已经以我现在听说过的完美状态存在,并且我也意识到使用R这样的语言很难自动地做一些这样的改变,但是一个人可以做梦吧?有人在所有这些指针上有指针吗?

I'm guessing that if such a tool already existed in a perfect state that I'd have heard of it by now, and I'm also realizing that with a language like R it's difficult to do some of these sorts of changes automagically, but one can dream, right? Does anyone have pointers on some/all of these?

推荐答案

由于这似乎仍然很有意义,我想提一下样式器,它根据 tidyverse样式

Since this is still seem relevant I thought to mention styler which reformats r code according to the tidyverse style.

它会打勾您的一些盒子,例如基本格式设置,但不重命名变量(尽管linter lintr 至少能够显示这些内容)。

It ticks some of your boxes e.g. basic formatting but doesn't rename variables (although the linter lintr at least is able to show those).

Styler作为R包提供,具有接受代码的功能(例如 style_text(),但可以使用在命令行上也是如此:

Styler comes as an R package with functions the accept code (e.g. style_text(), but it can be used on the command line as well:

例如 tmp.r

a <-c(1,2,3) 
if(foo) {
  b=2 }
myVar=2

并运行:

Rscript -e 'styler::style_file("tmp.r")'

将tmp.r覆盖为:

a <- c(1, 2, 3)
if (foo) {
  b <- 2
}
myVar <- 2

这篇关于R语言意识的代码重新格式化/重构工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆