Once upon a time I noticed that most of my .r files started the same way:
set directory, read data, load packages, summarize data.  Being a lazy
programmer, I figured out a way to automate this workflow with
"auto-insert-mode".  A brief introduction is here:
http://www.emacswiki.org/emacs/AutoInsertMode

Now, when I open a new .r file, the following text (for example) is
automatically inserted into the buffer:

# test.r
# Time-stamp: <>
library(Hmisc)
library(lattice)
library(reshape)
setwd("c:/projects/")
dat <- read.csv("")
dat <- transform(dat, rep=factor(rep))
str(dat)
describe(dat)

Here is the relevant code for .emacs:

(load-library "autoinsert")
(setq auto-insert-query nil) ; Don't prompt user for autoinsert header
(add-hook 'find-file-hooks 'auto-insert)
(require 'ess-site)
; Autoinsert for ESS.  This needs to be done after ESS is loaded
(defun my-insert-ess-header ()
  "Inserts the header for ess-mode files."
  (interactive)
  (if (string= ess-language "S")
      (progn
        (insert (concat "# " (file-name-nondirectory buffer-file-name)
"\n"))
        (insert "# Time-stamp: <>\n\n")
        (insert "library(Hmisc)\n")
        (insert "library(lattice)\n")
        (insert "library(reshape)\n")
        (insert "setwd(\""
                (file-name-directory buffer-file-name)
                "\")\n")
        (insert "dat <- read.csv(\"\")\n")
        (insert "dat <- transform(dat, rep=factor(rep))\n")
        (insert "str(dat)\n")
        (insert "describe(dat)\n")
)))
(add-to-list 'auto-insert-alist '((ess-mode . "ESS mode") .
my-insert-ess-header))

Note: It would probably be easier to maintain this if I had a template.r
file that was auto inserted.  See the wiki above.

I also find auto-insert very useful for setting up a basic latex document
when opening a new .rnw file.  I leave that as an exercise for the reader.
 :-)

Maybe people have already discovered this, but I didn't find anything after
a quick search at Rseek.

Kevin Wright

	[[alternative HTML version deleted]]

