Skip to content

Instantly share code, notes, and snippets.

@keithshep
Created June 11, 2010 02:12
Show Gist options
  • Save keithshep/433946 to your computer and use it in GitHub Desktop.
Save keithshep/433946 to your computer and use it in GitHub Desktop.
reformat R source
#!/usr/bin/Rscript
library("animation")
# reformat a directory full of R source files
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(".*\\.[rR]$", currFile, perl = T))) {
outFile <- file.path(outDir, f)
if (file.exists(outFile))
stop(paste("refusing to overwrite", outFile))
tidy.source(currFile, file = outFile, ...)
}
}
}
cmdArgs <- commandArgs(trailingOnly=T)
if(length(cmdArgs) != 2)
stop("Usage: format-r.R IN_DIR OUT_DIR")
tidy.all(cmdArgs[1], cmdArgs[2], width.cutoff = 80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment