knitr for ninja

Yihui Xie @ New York R Users Group
09/10/2013

Teachers day (China)

Big thanks to Prof Xizhi Wu (School of Statistics, Renmin University of China), the first person who introduced R to Renmin Univ in 2001

Not a ninja yet?

Okay, a brief introduction

knitr as a package

library(knitr)
knit('your-document.Rmd')  # compiles document
purl('your-document.Rmd')  # extracts code

The spin() function

  • give me an R script, and I give you a report
  • internally converted to R Markdown/LaTeX/…
#' today I built a model
fit = lm(dist ~ speed, data = cars)

#' and I got the slope `r coef(fit)[2]`

#+ dist-speed, fig.width=5, fig.height=4
plot(cars)
abline(fit)

The stitch() function

  • insert an R script in a predefined template
  • LaTeX, HTML or Markdown

R Markdown

  • if you cannot master the basic syntax of Markdown in 10 minutes, I give you 10 dollars
  • RPubs: http://rpubs.com
    • forget about reproducible research, because you are doing it unconsiously
  • R Presentations: coming in RStudio 0.98
  • a common misunderstanding about knitr: knitr == R + Markdown (Sweave == R + LaTeX)
    • much more time was spent on developing LaTeX features
    • knitr == (any programming language) + (any document language) (in theory, of course!)

Blogging: Jekyll or WordPress

R Package vignettes

Docco style (knitr's Gangam style)

# see list of vignettes
help(package = 'knitr', help_type='html')
# Docco Classic Style

The knitr book

Pandoc

reStructuredText and AsciiDoc

Language engines

  • the chunk option engine
awk bash coffee gawk haskell perl python
Rscript ruby sas sed sh zsh highlight
Rcpp tikz dot c asy cat
  • a python example
x = 'hello, world'
print(x.split(', '))
['hello', 'world']

The runr package

Learn what other cool kids are doing

Questions?