Get up and running with R, Sweave, and LaTex

July 2, 2010
By

(This article was first published on Brock's Data Adventure » R, and kindly contributed to R-bloggers)

There are a lot of great references on the web on how to levarage LaTeX for reporting and presentations (beamer), but as someone who is completely new to R and reproducible research, I was having a pretty hard time figuring out simply what to do (what tools, where do you edit, etc.). Eclipse as a programming environment has been growing on me, but I simply could not get it to render me a PDF.

In this post, I am going to link to a few resources and attempt to explain it to you in a way that hopefully will get you up and running.  It really is pretty straight forward.  Learning how to modify commands, however, thats another post for another day.

Assumptions:
You have R up and running (I am using 2.10.1)
You are just trying to create a document – I tried to create a presentation (Beamer) and got an error when trying to open the PDF….I will try to figure this out later

First, save the following text below as a .rnw file

@found at http://www.math.ucla.edu/~getreuer/latex.html
documentclass[12pt]{article}
begin{document}

The #1 story is that this article was compiled today (today) with
LaTeX.  To try some commands, it includes textit{a few fancy}
textbf{fonts}.

end{document}

Here is the code:

### Getting started with Sweave in R
### helpful links
# My favorite
# http://biostat.mc.vanderbilt.edu/wiki/pub/Main/TheresaScott/ReproducibleResearch.TAScott.handout.pdf
# Others
# http://www.wekaleamstudios.co.uk/posts/creating-a-basic-presentation-using-latex-beamer
# http://jeromyanglim.blogspot.com/2010/02/getting-started-with-sweave-r-latex.html

### Simply, LaTeX is just formatting embedded inside of your text
### You can take templates online and save/edit in Notepad, just save as .nw
### The process is simple, text editor, save as .nw, run Sweave to generate .tex
### , compile in R

### There is also a stand alone program MikTex that will compile for you as well

### My basic code:

#### set the working diretory and check it
setwd("C:/Users/You/Desktop/Test Sweave/")
getwd()

#### Sweave is a function that will work on the .nw file
#### ?Sweave
Sweave("Test.nw")

####  You should see some code saying
####  Writing to file Test.tex
####  You can now run LaTeX on 'test.tex'
####  Now we simply have to compile it
tools::texi2dvi("Test.tex", pdf=TRUE)

####  You can also load the tools library first - shorter way above
####  library(tools)
####  texi2dvi("Test.tex", pdf=TRUE)

####  Voila, its that easy. No need to bother with eclipse plugins
####  The PDF will be saved to the working directory

One Note:  You may get prompted to install some additional packages…I said Yes.


Filed under: R, Tutorial Tagged: LaTex, R, Sweave, Tutorial

To leave a comment for the author, please follow the link and comment on his blog: Brock's Data Adventure » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.