Script to indent R code

I was looking for a way to indent my R code, since the R auto-packaging command inserted a bunch of hard tabs and anyways the code was messy.

I found this page describing a way to do it with ESS (Emacs Speaks Statistics). So, I downloaded ESS from the ESS homepage and adapted the script slightly to be a simple shell-script.

#!/bin/sh
# Use emacs ESS pkg to indent R file
# Dan Gunter, Dec 2008
function usage () {
printf "Indent R file with Emacs ESS package.\n"
printf "Usage: $0 FILE\n"
exit 1
}
f=$1
shift
if test "x$f" = x -o "x$f" = "x-h"; then
usage
fi
emacs -batch \
-eval '(load "/Users/dang/local/lib/emacs/ess-5.3.8/lisp/ess-site")' \
-f R-mode \
-eval '(insert-file "'${f}'")' \
-eval '(set-visited-file-name "'"${f}"'")' \
-eval '(indent-region (point-min) (point-max) nil)' \
-f save-buffer \
2>/dev/null

2 Comments

dangDecember 2nd, 2008 at 4:11 pm

Doh! A simpler and probably better way is documented in the R extension manual (section 3.1)

options(keep.source = FALSE)
source(”myfuns.R”)
dump(ls(all = TRUE), file = “new.myfuns.R”)

VaudtVarkenFebruary 8th, 2009 at 2:39 am

@dang
your solution doesn’t indent the code, it only dumps the variables….

Leave a comment

Your comment

  WordPress version 2.8.4