views:

1087

answers:

5

I'm looking to streamline my Sweave document creation, and I'd like to hear about people's current setups. I feel like the holy grail goes something like this:

  • Editing Rnw code on one half of the screen
  • Single keybinding compiles Sweave document and runs pdflatex
  • View PDF on the other half of the screen; once compiled, PDF is refreshed and centered around the portion of the document you're editing
  • If compilation has errors, replace the PDF with the results of the compilation (e.g. latex errors or Sweave errors)

I am guessing/hoping that the solution is part Emacs/ESS combined with some code for the Emacs profile and/or a nice Makefile. But I would really like to hear about everybody's preferred way of creating Sweave and/or Latex documents.

+4  A: 

A few other R users I talked to use a 'one-directory-per-project' setup, and a simple Makefile. As you suspected, that works well with Emacs/ESS.

I tend to just call a simple shell script sweave which I wrote before before 'R CMD Sweave' was added (as I find re-creating or copying the Makefile unappealing, YMMV). I also use Emacs and an auto-refreshing pdf viewer (like okular or kpdf). Emacs23 can preview pdf files directly too but I have yet to switch my work flow to that.

edd@ron:~$ cat bin/sweave
#!/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\")" \
      | R --no-save --no-restore --slave

LATEXFILE=$BASENAME.tex
filetest $LATEXFILE && pdflatex $LATEXFILE
Dirk Eddelbuettel
+2  A: 

You can do everything that you suggest there with the StatET plugin for Eclipse. That's what I use for Sweave development; it understands both latex and R very well, including syntax highlighting, etc.

You can get it here: http://www.walware.de/goto/statet.

Longhow Lam has written a nice guide: http://www.splusbook.com/Rintro/R%5FEclipse%5FStatET.pdf.

http://www.statalgo.com/?p=93

Shane
I agree. Sweave, StatET, and Eclipse work well together. I wrote a post describing one way of configuring Sweave with Eclipse http://bit.ly/adfhZ1
Jeromy Anglim
A: 

I use the "one-directory-per-project" and Makefile approach as well. I also include commands to create output in HTML, which can then be converted to OOo and MS Word, using tth. This is important for me since a lot of my collaborators are MS Office users and are resistant to using the PDF output. I learned a lot about how to do this from Frank Harrell's twiki at Vanderbilt.

Personally I use gvim as my editor of choice and running make from there is quite simple, as it is from Emacs.

Abhijit
A: 

I use TeXShop on OS X to produce all of my LaTeX and Sweave reports. For me, a new compilation pipeline is as simple as adding a file, called Sweave.engine to ~/Library/TeXShop/Engines/ which contains the following:

#!/usr/bin/env Rscript
args <- commandArgs(T)

fname <- strsplit(args[1],'\\.')[[1]][2]

Sweave(paste(fname,'Rnw',sep='.'))

system(paste('pdflatex',paste(fname,'tex',sep='.')))

Sweave is now a selectable method of compiling a document inside TeXShop. I can set it to be the default for a document by adding the following TeX hash-bang to the top of the file:

% !TEX TS-program = Sweave

Hitting Cmd-T will typeset the document- the pdf automatically pops up in a separate window. TeXShop also incorporates SyncTeX technology so a Cmd-Click in the Rnw source will highlight the corresponding output in the PDF window and a Cmd-Click in the PDF window will highlight the corresponding input in the Rnw source.

TeXShop is mac-only but a great Qt/poppler-based clone, TeXworks, is available for Linux, Windows and Mac and supports many of the same features-- including TeX hash-bangs and SyncTeX. TeXworks has reached a level of maturity where it is included in version 2.8 of the MikTeX package for Windows. A ready-to-go Application Bundle is also available for Linux.

Next time I am using TeXworks on Windows and Linux, I will post an equivalent set of commands for adding Sweave to the list of available typesetting pipelines.

Sharpie
A: 

One thing that has saved me some time is the 'auto-insert' mode in emacs. I have it set up so that each time I open a new .rnw file, emacs automatically sets up a basic document template and all I need to do is start writing my report.

kevin