knitr: A flexible R authoring toolknitrFrom 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.
knitr\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}
<<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)
@
p.fig.lp and the chunk name, in this case fig:erupt.knitrknitrChunk 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))
}
knitrA highly articulated, but easy enough to use system of code decoration and output styling.
# 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>
knitrresults="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.
knitrFunctions 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>")
}
})
knitrSetting the following code chunk options.
<<tobesmall, really_small_text = T, results = asis
>>
Run the following code:
summary(faithful)
eruptions waitingknitrE.g. I wrote up these workshop notes (almost) entirely in R Markdown.
knitrYou’re looking at it.
knitrknitrMy workflow:
knit(index.Rmd)system("pandoc -s -S --self-contained -H css_and_js.html -t dzslides --mathjax index.md -o index.html")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.
knitr