Get up and running with R, Sweave, and LaTex

[This article was first published on Brock's Data Adventure » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: Brock's Data Adventure » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)