William Liu

Report Generation with R, Markdown, LaTeX


##Table of Contents

##Summary

R normally executes code in a regular R script. However, sometimes we don’t want just the code and instead want to create a report that has some text documentation along with the R code. By default, we can use the Sweave package (included with every R install) and we can add additional dynamic report generation using the knitr package. The idea is to combine program source code (code chunks) and the corresponding documentation (documentation chunks) into a single file.

Code is normally stored as a R Script (e.g. .R file). For the documentation portion, we use mainly Markdown (e.g. .md, .rmd, i.e. anything with md) or LaTeX (.tex). We use a library like Sweave or knitr to put together the code chunks in R’s noweb format (.rnw) along with the documentation (in Markdown and LaTeX) to create an output (e.g. HTML, PDF).

##Setup

I downloaded R, and RStudio, Sublime Text 3 with the LaTeXTools Package (so I can edit with my text editor of choice). On a Mac, I downloaded MacTex and on a PC I downloaded MiKTeX. The MacTex and MiKTeX are package managers for the LaTeX typesetting (i.e. so you can use the \usepackage{stuff} command, which imports helpful packages for things like importing images, if-then statements)

##R Sweave

Sweave creates dynamic reports using Markdown or LaTeX, a high-quiality typesetting system mainly used in scientific documentation.

####LaTeX showing code using Sweave

If you were writing your documentation in LaTeX, Sweave creates code blocks using << myoptions >>= to begin a code block and @ to end a code block. The output can be the code itself or the results of the code (e.g. a graph of the output). The main options are fig (which tells us if we should show the plot) and echo (which tells us if we should show the R code). We then click the ‘Compile PDF’ button. If you want to run any R code inside, just highlight the code chunks and click ‘Run’.

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<name="Stuff", fig=TRUE, echo=FALSE>>=

cat("This is my R code.\n")
set.seed(123)
x <- 1:10
y <- rnorm(10)
plot(x, y)

cat("This is some more R code.\n")

@

\end{document}

####LaTeX showing plots using Sweave

####Markdown showing code using Sweave

If you were writing your documentation in Markdown, you start a code chunk with the below marks. If you do not want to specify the underlying R code, use echo=FALSE. If you do not want to display the graph, use eval=FALSE.

```{r, echo=FALSE}
1+1
.4-.7+.3  # What?  Why is this not zero?!
```

# R code expressions
Yes I know the value of pi is `r pi` and 2 times pi is `r pi*2`

####Markdown showing plots using Sweave

Again, you can also show plots in Markdown.

 ```{r, echo=FALSE}
plot(1:10, 1:10)
 ```

##R knitr

####knitr Setup

knitr is a package (like Sweave) that is another way of ‘weaving’ (i.e. replace code snippets with the generated code output). It has a few more advanced features like being able to generate the printed R terminal (showing warnings and errors), more flexibility on graphics, etc.

Using RStudio, install the package ‘knitr’ on CRAN. Then change RStudio’s settings under ‘Tools’ > ‘Global Options’ > ‘Sweave’ > ‘Weave Rnw files using’ > switch from ‘Sweave’ to ‘knitr’.

####LaTeX and Markdown showing code using knitr

knitr is included as code chunks just like Sweave. The only thing is to include library(knitr) right at the front of the code chunk.

LaTeX

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}
<<name="Stuff", fig=TRUE, echo=FALSE>>=
    library(knitr)
    cat("This is some more R code.\n")
@
\end{document}

Markdown

```{r, echo=FALSE}
library(knitr)
1+1
.4-.7+.3  # What?  Why is this not zero?!
```