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.
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 package will help: http://yihui.name/en/?s=formatr.