Archive for March 2010
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:
- You have two things: your source data file and your R code.
- 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.
- 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.
- You run
pdflatexagainst 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.
- Download and install LaTeX.
- Download and install R.
- Reboot. Probably not necessary, but I’d do it anyhow.
- Open Terminal.
mkdir -p ~/Library/texmf/tex/latex
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.
- Create a folder on the Desktop called:
- Launch R.
- 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.
- Open Terminal.app
- 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!