An Introduction to knitr

Yihui Xie
2013/07/17 @ Seattle, BioC 2013

My first BioC conference


  • motivation (a real one “better homework” and a correct one “reproducible research”)
  • features
  • discussion of remaining problems
  • see the lab session by Jeff Johnston on Friday for practical applications

Forget about the big words for now...

  • literate programming
    • designed for software development
    • heaven for reporting (Sweave, ingenious!)
    • boring for software (LaTeX package/doc)
  • reproducible research
    • sounds like “more work to do”?
    • I do not want to learn anything new; let me copy and paste
  • one day they will be doing reproducible research unconciously

Consider some practical problems

  • the problem of “when you type a backslash, God kills a kitten”
    • how many kittens were killed after I taught them LaTeX?
  • the problem of “disappearing summer interns” (coined by Carlos Scheidegger)
  • one of the best comments in source code
# When I wrote this, only God and I understood what I was doing
# Now, God only knows

Solution: Marmot!

Solution: Markdown!

Solution: knitr

  • any computing language (not yet)
    • R, Python, Perl, C++, Shell scripts, awk, sed, CoffeeScript, …
    • even SAS
  • any authoring language
    • LaTeX, HTML, Markdown, reSructuredText, AsciiDoc

One-minute introduction

What are you waiting for?

Everything about knitr

The knitr book (coming on July 22)

Basic features

  • R + Markdown, R + LaTeX, R + HTML
  • numeric output, graphics (also animations), syntax highlighting, code formatting, …


  • if you do not change anything, just load the old results
  • lazyLoad()


  • use the engine option, e.g. engine = 'python', engine = 'awk'
  • see knitr-examples/.*engine.*.Rmd (e.g. 023)


  • current design: for each chunk, use a new process, e.g. system('python -c "print(1+1)"')
  • Doug Bates: engine = Julia?
  • how do I open a persistent sub-process in R, pipe code into it, and get the results back?
    • socket?
    • fifo?


R Notebook

Run this locally:

# please use the latest deveopment version on Github
if (!require('knitr') || packageVersion('knitr') <= '1.3') install.packages('knitr', repos = c('', ''), type = 'source')
demo('notebook', package = 'knitr')


R Presentations (RStudio)

Package vignettes