19

I've now got everything to work properly on my Mac OS X 10.6 machine so that I can create decent looking LaTeX documents with Sweave that include snippets of R code, output, and LaTeX formatting together. Unfortunately, I feel like my work-flow is a bit clunky and inefficient:

  1. Using TextWrangler, I write LaTeX code and R code (surrounded by <<>>= above and @ below R code chunk) together in one .Rnw file.

  2. After saving changes, I call the .Rnw file from R using the Sweave command

    Sweave(file="/Users/mymachine/Documents/Assign4.Rnw", 
            syntax="SweaveSyntaxNoweb")
    

    In response, R outputs the following message:

    You can now run LaTeX on 'Assign4.tex'

    So then I find the .tex file (Assign4.tex) in the R directory and copy it over to the folder in my documents ~/Documents/ where the .Rnw file is sitting (to keep everything in one place).

  3. Then I open the .tex file (e.g. Assign4.tex) in TeXShop and compile it there into pdf format. It is only at this point that I get to see any changes I have made to the document and see if it 'looks nice'.

Is there a way that I can compile everything with one button click? Specifically it would be nice to either call Sweave / R directly from TextWrangler or TeXShop. I suspect it might be possible to code a script in Terminal to do it, but I have no experience with Terminal.

Please let me know if there's any other things I can do to streamline or improve my work flow.

11 Answers 11

12

I use a Makefile of the following form for my Sweave documents:

pdf: myfile.tex
    R CMD texi2pdf myfile.tex

myfile.tex: myfile.Rnw
    R CMD Sweave myfile.Rnw

Then I can build the document in one step in the Mac OS Terminal by running the command make pdf

I'm sure there is a way to bring this closer to your one-click goal in Mac OS X, but this works well enough for me.

1
10

One-click Sweaving is easy to do in TeXShop using the Sweave.sh script by Gregor Gorjanc. Get it from http://cran.r-project.org/contrib/extra/scripts/Sweave.sh and put it in your ~/Library/TeXShop/bin/ folder.

Then add the following files to your ~/Library/TeXShop/engines/ folder:

As Sweave.engine:

#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh  -ld "$1"

As SweaveNoClean.engine:

#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh  -nc -ld "$1"

You'll have to set the permissions on Sweave.sh and the two engine files to allow execution.

To Sweave with one click, restart TeXShop after adding these files, open the Sweave document (with Rnw extension) and in the dropdown menu above the document window, change it from LaTeX to Sweave or SweaveNoClean.

BEWARE: The "Sweave" option wll clean up after itself, deleting all the extra files LaTeX and Sweave creates. If your file is called myfile.Rnw, this will include files called myfile.R and myfile.tex. So a word to the wise: make sure the basename of your Rnw file is unique; then nothing unexpected will be written over and then deleted.

The SweaveNoClean option does not clean up after itself. This makes sure you don't delete anything unexpected; though it could still write over a file called myfile.tex if you Sweave a myfile.Rnw. This also doesn't delete any graphics that have been created, in case you want to have them separate from your full typeset document.

12
  • Thanks Aaron; I followed your instruction; however still no luck. The TeXShop console spits out two lines (after compiling with Sweave option in the dropdown): /Users/kg/Library/TeXShop/Engines/Sweave.engine: line 2: /Users/kg/Library/TeXShop/bin/Sweave: No such file or directory
    – baha-kev
    Feb 7, 2011 at 19:27
  • @user594795: Try modifying the two engine files to say "Sweave.sh" instead of just "Sweave"; I've edited my answer accordingly. Feb 7, 2011 at 19:32
  • You mentioned above I need to set the permissions. I took that to mean changing all the files to "read & write" after clicking "Get Info" on the files. The reason I ask is that now I am getting the following message (still in the TeXShop console): /Users/kg/Library/TeXShop/Engines/Sweave.engine: line 2: /Users/kg/Library/TeXShop/bin/Sweave.sh: Permission denied
    – baha-kev
    Feb 7, 2011 at 20:45
  • The permissions you need to set are read and execute. From the command line, it's chmod +x <filename> to add the execute permission. Feb 7, 2011 at 21:21
  • It now works great -- thank you for your help and patience! I'm happy to finally be learning about the new world of the Terminal.
    – baha-kev
    Feb 7, 2011 at 23:01
8

On the bash shell command line:

R CMD Sweave foo.Rnw && pdflatex foo.tex

Runs Sweave, and if that succeeds it goes on to do pdflatex. Out pops a pdf. If you've got this in a bash Terminal then just hit up-arrow to get it back and do it again. And again. And Again.

Makefile solution also good.

2
  • 3
    I use this too but a bit longer (also you can drop file extensions). Basically it just adds bibtex in the chain and opens the pdf when its done: R CMD Sweave foo && R CMD pdflatex foo && R CMD bibtex foo && foo.pdf Feb 7, 2011 at 17:48
  • Isn't it possible to say R CMD Sweave --pdf foo.Rnw to get a pdf directly? Nov 19, 2011 at 7:36
8

RStudio has a button that does this in one go. One caveat is that it runs in its own session, so any workspace variables you may have set are ignored.

the compile button

7

Just a note: you can actually call things like pdflatex etc. directly from R using texi2dvi (in the tools package). For example:

Sweave(file="/Users/mymachine/Documents/Assign4.Rnw")

texi2pdf("Assign4.tex")

would compile your Rnw file into a pdf. Thus, no need to leave R to handle the tex->pdf step.

2

I use these (saved as sweave.engine and sweavebibtex.engine) for custom engines in texshop. I usually work up a code chunk in R, then copy the block into the rnw file I have open in texshop. I'd love a solution that lets me do syntax highlighting and spelling correction of R and tex in the same document (that isnt emacs).

#!/bin/bash
echo 'SWEAVE | PDFLATEX. Custom engine--Akasurak-16Nov2009'
export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
pdflatex "${1%.*}"

and the second, for doing bibtex as well:

#!/bin/bash
date
before="$(date +%s)"
echo 'SWEAVE | PDFLATEX | BIBTEX | PDFLATEX | PDFLATEX. Custom engine--Akasurak-16Nov2009'
#Updated 20Jul2010 for auto including Sweave.sty


export PATH=$PATH:/usr/texbin:/usr/local/bin

R CMD Sweave "$1"
R CMD pdflatex "${1%.*}"
bibtex  "${1%.*}.aux"
R CMD pdflatex "${1%.*}"
R CMD pdflatex "${1%.*}"


after="$(date +%s)"
elapsed_seconds="$(expr $after - $before)"
date
echo Elapsed time '(m:s)': $(date -r $elapsed_seconds +%M:%S)

Can't say they are the best way of doing things, but they do work.

2

I use either Aquamacs or Eclipse to do the editing of the .Rnw file, then I use the following shell function to compile & view it:

sweaveCache () {
  Rscript -e "library(cacheSweave); setCacheDir(getwd()); 
    Sweave('$1.Rnw', driver = cacheSweaveDriver)" && 
  pdflatex --shell-escape $1.tex && 
  open $1.pdf
}

Notice that I'm using the cacheSweave driver, which helps avoid constantly re-executing code sections that take a long time to run.

BTW, I'm also trying to switch over to Babel instead of Sweave; not sure which I'll end up using more often, but there are definitely aspects of Babel that I like.

2

The best solution is here: you create a new *.engine for TeXShop to use, then typeset using the shortcut or the 1 button.

http://cameron.bracken.bz/sweave-for-texshop

Cameron is also very responsive, so I highly recommend his solution.

1

If you are open to switching to a (paid) solution, TextMate has a Sweave plugin that takes you from .Rnw to PDF in one step: Sweave, typeset, and view. Combined with Skim, which can be configured to reload PDFs, it makes tweaking files pretty easy.

1

I had this same issue (I use Mac OSX) and I opted to download Eclipse Classic 3.6.2. and then installed the StatET plugin. It's a bit hairy to get set up but once you do this environment is nice because you can one-click compile your .Rwn Sweave document using pdflatex and set options for your favorite viewer so the .pdf automatically pops up when you compile like it does in TeXShop. You can do this in TeXShop as well, but TeXShop is lousy for debugging .Rnw files and it doesn't highlight the R-code in the .Rwn file. In Eclipse you can customize the syntax highlighting (not the greatest from the Texclipse end, but ok) so that you can easily distinguish between your R and LaTeX code. You can also launch the R console from within Eclipse and it has a graphical object browser. Anyway, I could go on. If you want details about how to get it all installed, message me.

1

Guess I'm late to the party on this, but I put together a webpage that documents my Sweave workflow based on Eclipse (with one-touch sweave):

http://www.stanford.edu/~messing/ComputationalSocialScienceWorkflow.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.