**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:

<<name_of_chunk>>= 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.

\documentclass{beamer} \begin{document} \title{A Minimal Demo of knitr} \author{Yihui Xie} \maketitle \begin{frame}[fragile] You can test if \textbf{knitr} works with this minimal demo. OK, let's get started with some boring random numbers: <<boring-random>>= set.seed(1121) (x=rnorm(20)) mean(x);var(x) @ \end{frame} \begin{frame}[fragile] The first element of \texttt{x} is \Sexpr{x[1]}. Boring boxplots and histograms recorded by the PDF device: <<boring-plots,dev=pdf,fig.width=5,fig.height=5,out.width=.45\linewidth,par=TRUE>>= ## two plots side by side (option fig.show=hold) boxplot(x) hist(x,main='') @ \end{frame} \end{document}

**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...