Life as Clay

Learning R: Using Sweave on OS X 10.6 and fix the Sweave.sty problem

with 9 comments


I’m trying to teach myself R for a work-related project. The documentation is absolutely terrible for beginners, which is a shame, since the language is easy. Stuff that should take 1 minute takes 4 hours because there is a paucity of good tutorials on the Internet.

R comes with Sweave preinstalled. The concept is pretty cool. If you want to lay out your journal article or publication using LaTeX, then Sweave allows you to imbed the R code in the LaTeX document. It goes like this:

  1. You have two things: your source data file and your R code.
  2. You put the R code into a file that Sweave will process. That file can have several different extensions, like .Snw or .Rnw, for example. The Sweave file also includes your LaTeX code for formatting the final document.
  3. From within the R environment (this also can be done from the command line), you use the function Sweave() to process your R code and generate the .tex file. It automatically generates intermediate .eps and .pdf files of the components needed for the LaTeX output.
  4. You run pdflatex against your .tex file and end up with a pretty .pdf file, ready for publication.

This is a cool thing because it means that you can send the .Snw (Sweave) file to anybody and they automatically can reproduce the results with their own installation of R. As we know, reproducibility is a big deal for scientific studies.

There are a three major problems for the beginner:

  • R code is unnecessarily cryptic
  • The documentation is meant for people familiar with R and it’s TERRIBLE for beginners.
  • LaTeX is not installed by default on Macs.
  • Sweave runs fine out of the box, but LaTeX won’t be able to process the .tex files it generates because it will not be able to locate the Sweave.sty (style) file.

If you are a Mac user, this is what you have to do to use this system.

  1. Download and install LaTeX.
  2. Download and install R.
  3. Reboot. Probably not necessary, but I’d do it anyhow.
  4. Open Terminal.
  5. Type: mkdir -p ~/Library/texmf/tex/latex
  6. Type: cd ~/Library/texmf/tex/latex
  7. Type: ln -s /Library/Frameworks/R.framework/Resources/share/texmf Sweave
    What this does is create a symbolic link to Sweave that LaTeX can find so that you don’t get the error message from LaTeX about not finding Sweave.sty.

That’s it. You’re ready to use Sweave. Here’s how you can test it. This is what I did, just so that I could find the files more easily. You can put your working directory anywhere you want.

  1. Create a folder on the Desktop called: routput
  2. Launch R.
  3. Enter the following code:
# Clear variables from your environment, in case you were doing other work.
rm(list=ls())

# Set the working directory to the folder you created on the Desktop - replace 'clay' with your username
setwd("/Users/clay/Desktop/routput")

# Load the Sweave test file into the testfile variable
testfile <- system.file("Sweave", "Sweave-test-1.Rnw", package = "utils")

# Run Sweave against the test file
Sweave(testfile)

Now, take a look in the routput folder on your Desktop and you will see the files that Sweave generated. You need to run pdflatex to convert the .tex file to a .pdf file.

  1. Open Terminal.app
  2. Type: cd ~/Desktop/routput
  3. Type: pdflatex Sweave-test-1.tex
  4. Go look in the routput folder on the desktop to find your completed document

Next steps: locate the Sweave-test-1.Rnw file in your R structure and use it as a reference for doing your own Sweave work. The documentation on the Sweave homepage is pretty good.

I hope this helps you get started with Sweave!

Advertisements

Written by Clay

March 21, 2010 at 22:19

Posted in Code, R

Tagged with , , ,

9 Responses

Subscribe to comments with RSS.

  1. This “tutorial” might also help you get started:
    http://stat.epfl.ch/webdav/site/stat/shared/Regression/EPFL-Sweave-powerdot.pdf

    Clay

    March 21, 2010 at 22:29

  2. Thanks for making a clear post on how to create the link between Sweave and LaTeX.

    Joan-Josep Vallbé

    January 23, 2011 at 09:19

  3. This was very helpful. Thanks a lot. I have been trying to figure out this problem for the last couple of days.

    E. Schneider

    February 18, 2011 at 12:55

    • You’re welcome. It’s a shame that Sweave isn’t better integrated because it’s a fantastic feature. R would take over the world (faster) if it were a little more user friendly.

      Clay

      February 18, 2011 at 12:59

  4. Thanks so much for clarity, explanation, and brevity! This was fantastically helpful.

    Elizabeth

    July 23, 2011 at 23:19

  5. Thanks a lot for the post: clear and concise!

    Alessandro

    December 12, 2011 at 10:19

  6. Thanks for your post. I have .R file How do I create .Rnw file from .R? If I manually create .Rnw file and then use Sweave and pdflatex, it generates PDF file. Is there a better way to import R code into .Rnw file?

    Seema

    February 2, 2012 at 15:56

    • I think that you have to manually create the .Rnw file with copy/pasting R code. Not very efficient, but it’s the best way to get the layout that you desire.

      Clay

      February 2, 2012 at 20:32

  7. Great, many thanks!

    Sergio

    February 22, 2013 at 04:15


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: