Kei*_*ith 17 formatting r autoformatting
有没有可以自动重新格式化R代码的工具(编辑器,脚本,等等......)?它不需要是可自定义的,但它必须能够识别由分号或换行符分隔的语句,因为此代码具有两者.如果它可以将所有语句放在一个单独的行上,一致地缩进代码块并始终如一地放置括号,我将非常高兴.
编辑:总结调查结果
谢谢你的答案.这是我发现的.
这是我编写的一个小函数,以便我可以转换整个源目录(使用与formatR相同的底层函数,这在动画包中很奇怪).
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, ...)
}
}
}
Run Code Online (Sandbox Code Playgroud)