04/08/2014, Stat 416 Guest Lecture

About me

  • first language: Chinese
  • second language: R (10 years)
  • third language: English
  • graduated from Iowa State Univ (Stats, 2013)
  • joined RStudio
  • (co-)author and maintainer of some R packages (e.g. animation, knitr, cranvas, formatR, testit, highr, Rd2roxygen, fun, servr, tikzDevice, shiny, evaluate, markdown)


I know you click, click, copy and paste

But imagine you hear these words after you finished a project

Please do that again! (sorry we made a mistake in the data, want to change a parameter, and yada yada)

A real example (your homework 5)

Dynamic Documents


Document formats

  • computing language + documentation language
  • (R | Python | C++ | …) + (LaTeX | Markdown | HTML | …)
  • file extensions: .Rmd, .Rnw/.Rtex, .Rhtml, …

Reproducible research


As an R package

# install.packages('knitr')
knit("your-document.Rmd")  # compiles a document

Basic features

  • text output
  • graphics
  • cache
  • cross references
  • output hooks and chunk hooks
  • language engines

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

R Markdown

The old world

  • Sweave (R + LaTeX)
  • LaTeX produces high-quality PDF output, but when you introduce it to other people…

  • knitr supports LaTeX, though


  • 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 Markdown v1

  • primarily for HTML
  • the R package markdown

R Markdown v2

Very proud of R Markdown v2



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

Learn what other cool kids are doing


Hope you will enjoy it!