18

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.

Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93

6 Answers6

9

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
  • 360,940
  • 56
  • 644
  • 725
6

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_Eclipse_StatET.pdf.

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

Shane
  • 98,550
  • 35
  • 224
  • 217
  • 2
    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 Feb 23 '10 at 07:49
  • (splusbook link is broken. however I googled it and the following, seemingly equivalent, alternative, worked for me: http://www.splusbook.com/RIntro/R_Eclipse_StatET.pdf) – isomorphismes Sep 08 '11 at 21:39
6

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.

Sharpie
  • 17,323
  • 4
  • 44
  • 47
  • I like the direct use of Rscript! So many solutions invoke a shell to invoke R... For the record, the `strsplit` and `paste` for handling filename extensions is not necessary - both `Sweave` and `pdflatex` automatically add the extension. – Dav Clark Feb 11 '11 at 02:14
  • I think it was necessary at the time I wrote that post as TeX Shop passed the whole filename, `File.Rnw` to the engine. `Sweave` would be fine with it, but `pdflatex` would choke. – Sharpie Feb 11 '11 at 05:29
  • So - you say you have cmd-click between your .Rnw and .pdf files. Did you have to jump through these hoops also: http://cameron.bracken.bz/synctex-with-sweavepgfsweave-in-texshoptexworks – Dav Clark Jul 01 '11 at 23:54
  • No I didn't--so the SyncTeX support is probably a bit off. But I tend to write a lot of small Sweave chunks rather than bit ones, so SyncTeX lands me "in the ballpark" where I don't notice the issues associated with not using concordance. – Sharpie Jul 02 '11 at 00:18
3

Try RStudio.

I have been a fan of Emacs and TeXShop as mentioned in previous answers.

However, Rstudio is starting to win me over. It is a rapidly-improving dedicated IDE for R. Definitely worth checking out.

I still love doing certain R-only development tasks in the standard R IDE for mac. But for Sweave documents and some associated R devel at the same time, RStudio wins. It works with virtually zero tweaking. I'm not sure about the PDF-related features in the latter half of the original question.

Paul 'Joey' McMurdie
  • 7,295
  • 5
  • 37
  • 41
2

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.

Update: I've switched away from auto-insert. Now I use the "template.el" approach.

Kevin Wright
  • 2,397
  • 22
  • 29
1

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
  • 625
  • 1
  • 5
  • 9