自动格式化R代码的工具 [英] tool to auto-format R code

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

问题描述

有什么工具(编辑器,脚本,无论什么...)可用,可以自动重新格式化R代码?它不需要定制,但它必须能够识别由分号或换行符分隔的语句,因为这些代码都有。如果它可以把所有的语句放在一个单独的行上,一致地缩进代码块并且一直放置大括号,我将会非常高兴。

编辑:总结发现



感谢您的好评。这是我找到的。




  • ESS和StatET都是优秀的R编辑器,并且在自动缩进代码块方面做得很好。 StatET允许您一次选择全部并重新缩进文件中的所有内容。从我可以告诉ESS允许您一次缩进整个函数def,但不是整个文件(请纠正我,如果我错过了)。这些都不会固定大括号放置或分解多语句行。 (例如:i = n * b; a = i + 1)

  • formatR很棒。除了固定缩进之外,它还会一直放置大括号,并将多行语句分开。


这是我写的一个小函数,我可以转换整个源代码目录(使用与formatR相同的底层函数,这在动画包中很奇怪)。

  library 动画)

tidy.all< - function(inDir = NULL,outDir = NULL,...){
if(is.null(inDir)|| is.na (outDir))
stop(inDir不能为null或NA)
if(!file.info(inDir)$ isdir)
stop(inDir must be a directory )

if(is.null(outDir)|| is.na(outDir))
stop(outDir不能为null或NA)
if(! file.exists(outDir))
dir.create(outDir)$ b $ if(!file.info(outDir)$ isdir)
stop(outDir must be a directory)

for(f in dir(inDir)){
currFile< - file.path(inDir,f)
if(length(grep(。* \\\如果(file.exists(outFile))
stop(paste).R $,currFile,perl = T))){
outFile< - file.path(outDir,f) (拒绝覆盖,outFile))

tidy.source(currFile,file = outFile,...)
}
}
}


解决方案

尽管ESS是一个更好的长期解决方案,有一个快速格式化工作,也许这个包会帮助: http://yihui.name/en/?s = formatr


Is there any tool (editor, script, whatever...) available that can automatically reformat R code? It does not need to be customizable but it must be able to recognize statements separated by either semicolons or newlines since this code has both. If it can put all statements on a separate line, consistently indent code blocks and consistently place braces I will be very happy.

Edit: summarizing findings

Thanks for the great answers. Here is what I've found.

  • Both ESS and StatET are great R editors and do a nice job of auto indenting blocks of code. StatET allows you to select-all and re-indent everything in a file at once. From what I could tell ESS allows you to indent an entire function def at once but not the entire file (please correct me if I missed it). Neither of these will fix brace placement or break up multi-statement lines. (Eg: i = n*b;a=i+1)
  • formatR is awesome. In addition to fixing indentation it will also place braces consistently and split up multi-statement lines.

Here's a little function I wrote so that I can convert an entire source dir (using the same underlying function as formatR which is strangely in the animation package).

library("animation")

tidy.all <- function(inDir = NULL, outDir = NULL, ...) {
    if (is.null(inDir) || is.na(outDir)) 
        stop("inDir can't be null or NA")
    if (!file.info(inDir)$isdir) 
        stop("inDir must be a directory")

    if (is.null(outDir) || is.na(outDir)) 
        stop("outDir can't be null or NA")
    if (!file.exists(outDir)) 
        dir.create(outDir)
    if (!file.info(outDir)$isdir) 
        stop("outDir must be a directory")

    for (f in dir(inDir)) {
        currFile <- file.path(inDir, f)
        if (length(grep(".*\\.R$", currFile, perl = T))) {
            outFile <- file.path(outDir, f)
            if (file.exists(outFile)) 
                stop(paste("refusing to overwrite", outFile))

            tidy.source(currFile, file = outFile, ...)
        }
    }
}

解决方案

Although ESS is a much better long-term solution, if you just have a quick formatting job, perhaps this packge will help: http://yihui.name/en/?s=formatr.

这篇关于自动格式化R代码的工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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