Skip to content

Working comfortably with (X)Emacs + Sweave

4 messages · Tamas Papp, Dirk Eddelbuettel, Deepayan Sarkar

#
Dear List,

I am trying to become more familiar with Sweave at the moment, beacuse
I am convinced that it will eventually make my life easier. However,
I have not found anything relevant in the mail archives about the
following problem.

Both the article in R-News and the Sweave FAQ suggest that Emacs would
be a great development environment for working with Sweave. So far, it
doesn't seem to fit that description... Maybe I'm doing something
wrong. I am going to describe the way I do things, and hope that
people on this list can suggest something more comfortable.

I edit the file in one buffer and keep one open for the R prompt.
Every time I need to recompile the whole document (Sweave to, say,
DVI) I go through the following:

1. call sweave manually via the R prompt
2. open the resulting LaTeX file (I have not found a way to refresh
this, maybe there's some magical XEmacs keystroke that rereads the
file from the disk?, so I keep opening/closing it)
3. compile the LaTeX file

This is a bit tedious as I like to compile often, because I like to
see the way my document looks (I know that TeX is WYSIWYM, but I
prefer to check my formulas instantly).

I would appreciate if people told me how they solve these problems or
have found any other way to make Xemacs more comfortable. A
"call sweave and then latex on this file in one go" and "show the DVI
that was produced from this Rnw file" command would be very
useful. Emacs init.el files and similar would help a lot, too.

Or maybe I am just trying to use things the wrong way, if so, please
enlighten me ;-) If this is not how people use Sweave+Emacs, then tell
me how I should do it.

Thanks in advance,

Tamas Papp
#
On Wed, Apr 30, 2003 at 07:11:01PM +0200, Tamas Papp wrote:
Excellent question. Below is my pedestrian answer. I edit in XEmacs,
and then call this shellscript. You could also bind it to what gets 
called from latex/auctex when you 'compile' the input file.

Dirk


#!/bin/bash -e

function errorexit () {
    echo "Error: $1"
    exit 1
}	

function filetest () {
    if [ ! -f $1 ]; then
       errorexit "File $1 not found"
    fi
    return 0
}
		       
	       
if [ "$#" -lt 1 ]; then
    errorexit "Need to specify argument file"
fi
			   
		   
BASENAME=$(basename $1 .Rnw)
			   
RNWFILE=$BASENAME.Rnw
filetest $RNWFILE
echo "library(tools); Sweave(\"$RNWFILE\")" \
      | Rterm --no-save --no-restore --slave
			       
LATEXFILE=$BASENAME.tex
filetest $LATEXFILE && pdflatex $LATEXFILE
			       
PDFFILE=$BASENAME.pdf
filetest $PDFFILE && acroread
#
On Wed, Apr 30, 2003 at 12:41:18PM -0500, Dirk Eddelbuettel wrote:
Sorry - the last line lacks "$PDFFILE &" missing, and I forgot to mention
that this is the Cygwin version of the script, where acroread is another
wrapper that calls the pdf viewer. A Unix version would use R instead of
Rterm.

Dirk
#
Assuming that you work on GNU/Linux, here's my setup (mostly borrowed from 
Doug Bates):

My .emacs has (apart from the usual ESS and auc-tex stuff):


(defun Rnw-mode ()
  (require 'ess-noweb)
  (noweb-mode)
  (if (fboundp 'R-mode)
      (setq noweb-default-code-mode 'R-mode)))
(add-to-list 'auto-mode-alist '("\\.Rnw\\'" . Rnw-mode))
(add-to-list 'auto-mode-alist '("\\.Snw\\'" . Rnw-mode))
(setq reftex-file-extensions
      '(("Snw" "Rnw" "nw" "tex" ".tex" ".ltx") ("bib" ".bib")))
(setq TeX-file-extensions
      '("Snw" "Rnw" "nw" "tex" "sty" "cls" "ltx" "texi" "texinfo"))



I have the following script installed:

deepayan 12:59:03 $ cat /usr/local/bin/Sweave.sh
#!/bin/sh
echo "library(tools);Sweave('"$1"')" | /usr/bin/R --vanilla --silent




In the working directory, I have a Makefile similar to:

deepayan 13:01:15 $ cat Makefile

DEFAULT_PDF = filename
DEFAULT_PS = filename

all : $(DEFAULT_PDF:=.pdf) $(DEFAULT_PS:=.ps)

%.tex : %.Rnw
        Sweave.sh $<

%.pdf : %.tex
        texi2dvi --pdf $<

%.dvi : %.tex
        texi2dvi $<

%.ps : %.dvi
        dvips -o -q $<




With all this (customized according to your tastes), just edit the 
filename.Rnw file, and invoke make (I tend to keep a gv open in 'watch file' 
mode).

Hope that helps.

Deepayan
On Wednesday 30 April 2003 12:11 pm, Tamas Papp wrote: