R and presentations: a basic example of knitr and beamer

February 12, 2012

(This article was first published on NumberTheory » R stuff, and kindly contributed to R-bloggers)

Manually combining R code and a presentation can be quite a pain. Luckily, using tools like odfWeave, Sweave and knitr, integrating documents and R code is quite painless. In this post I want to take a look at combining the knitr package with the Latex package beamer. I use the knitr package instead of the the Sweave package because it basically is a better Sweave, see this link for more information.

The basic structure of a knitr document is that you fill your text file with latex code, interspaced with section of R code like this:

some R code here

It is custom to save this file with a .Rnw extension. Going from the Rnw to a pdf is a two step procedure: 1) run knitr, and 2) run pdflatex. First, knitr interprets the R code to produce a tex file, and then pdflatex creates the pdf. Below I posted a basic example which shows how to use knitr together with the beamer document class. The resulting pdf looks like this, and this is the knitr/latex code (Rnw file) that was the source.



\title{A Minimal Demo of knitr}
\author{Yihui Xie}


You can test if \textbf{knitr} works with this minimal demo. OK, let's
get started with some boring random numbers:


The first element of \texttt{x} is \Sexpr{x[1]}. Boring boxplots
and histograms recorded by the PDF device:

## two plots side by side (option fig.show=hold)


To leave a comment for the author, please follow the link and comment on their blog: NumberTheory » R stuff.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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...

Comments are closed.

Search R-bloggers


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)