Life as Clay

Archive for March 2010

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.

# Set the working directory to the folder you created on the Desktop - replace 'clay' with your username

# 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

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
  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!


Written by Clay

March 21, 2010 at 22:19

Posted in Code, R

Tagged with , , ,