knitr: A flexible R authoring tool

Josef Fruehwald

Philly UseR, Sept 2012

knitr

From the knitr homepage:

The knitr package was designed to be a transparent engine for dynamic report generation with R, solve some long-standing problems in Sweave, and combine features in other add-on packages into one package.

Using knitr

Figures in Sweave

\SweaveOpts{width = 8, height = 5}
\setkeys{Gin}{width = 0.8}

\begin{figure}[htbp]
\centering
<<chunk1>>=

  p <- qplot(x = waiting, y = eruptions, data = faithful)
  print(p)

@
\caption{Eruptions}
\label{fig:erupt}
\end{figure}

Figures in knitr

<<erupt, fig.width = 8, fig.height = 5, out.width = 0.8\\textwidth, fig.align = center,fig.pos = htbp,fig.cap = Eruptions,fig.lp = fig:, dev = pdf>>=

  p <- qplot(x = waiting, y = eruptions, data = faithful)

@

Using knitr

Animations!

Using knitr

Animations!

Chunk options:

<<noise, fig.show = animate, aniopts=controls, fig.width=3, fig.height=3, dev = svg, echo = F>>

R code:

for (i in 1:10) {
    plot(rnorm(100), rnorm(100))
}

Using knitr

A highly articulated, but easy enough to use system of code decoration and output styling.

Default
# A comment
print(xtable(summary(faithful)), type = "html")
## <!-- html table generated in R 2.15.1 by xtable 1.7-0 package -->
## <!-- Thu Sep 27 18:11:34 2012 -->
## <TABLE border=1>
## <TR> <TH>  </TH> <TH>   eruptions </TH> <TH>    waiting </TH>  </TR>
##   <TR> <TD align="right"> 1 </TD> <TD> Min.   :1.60   </TD> <TD> Min.   :43.0   </TD> </TR>
##   <TR> <TD align="right"> 2 </TD> <TD> 1st Qu.:2.16   </TD> <TD> 1st Qu.:58.0   </TD> </TR>
##   <TR> <TD align="right"> 3 </TD> <TD> Median :4.00   </TD> <TD> Median :76.0   </TD> </TR>
##   <TR> <TD align="right"> 4 </TD> <TD> Mean   :3.49   </TD> <TD> Mean   :70.9   </TD> </TR>
##   <TR> <TD align="right"> 5 </TD> <TD> 3rd Qu.:4.45   </TD> <TD> 3rd Qu.:82.0   </TD> </TR>
##   <TR> <TD align="right"> 6 </TD> <TD> Max.   :5.10   </TD> <TD> Max.   :96.0   </TD> </TR>
##    </TABLE>

Using knitr

results="asis"

# A comment
print(xtable(summary(faithful)), type = "html")
eruptions waiting
1 Min. :1.60 Min. :43.0
2 1st Qu.:2.16 1st Qu.:58.0
3 Median :4.00 Median :76.0
4 Mean :3.49 Mean :70.9
5 3rd Qu.:4.45 3rd Qu.:82.0
6 Max. :5.10 Max. :96.0

The googleVis library, for example, will print out the code for a motion chart as html, and with results="asis", you can embed it directly into the webpage.

Using knitr

Hooks

Functions to run before and/or after a code chunk is evaluated.

knit_hooks$set(really_small_text = function(before, options, envir) {
    if (before) {
        return("<div style=\"font-size:5pt;\">")
    } else {
        return("</div>")
    }
})

Using knitr

Hooks

Setting the following code chunk options.

<<tobesmall, really_small_text = T, results = asis>>

Run the following code:

summary(faithful)
eruptions waiting
Min. :1.60 Min. :43.0
1st Qu.:2.16 1st Qu.:58.0
Median :4.00 Median :76.0
Mean :3.49 Mean :70.9
3rd Qu.:4.45 3rd Qu.:82.0
Max. :5.10 Max. :96.0

Using knitr

R markdown!

E.g. I wrote up these workshop notes (almost) entirely in R Markdown.

Using knitr

RPubs

You’re looking at it.

Using knitr

html5 Slides

Benefits
Downsides

Using knitr

html5 Slides

My workflow:

There’s also the slidify library, but it doesn’t seem quite ready for prime time. Documentation is a little thin, and currently only supports deck.js and html5slides. But the author says he’s making progress on a new version.

Using knitr

Other nice features