Skip to content

Instantly share code, notes, and snippets.

Last active February 3, 2016 16:57
Show Gist options
  • Save pandafulmanda/175337758574f5fe5c91 to your computer and use it in GitHub Desktop.
Save pandafulmanda/175337758574f5fe5c91 to your computer and use it in GitHub Desktop.


HRUG Meetup

Why Knitr

  • results need to be reproducible
  • Documentation of code
    • easier to read and understand later on to extend
  • Great for making reports as well
  • Sharing results leads to facilitating discussion


Donald Knuth

  • On literate programming:
    • ..."consider programs to be works of literature"
    • comprehensible programs --> better programs
  • Also, because he's badass, made Tex for proper typesetting.

Some neat trivia

DK made WEB

  • WEB takes source file (of code and text)
  • has 2 main functions
    • Tangle
      • compiles to an executable code file
    • Weave
      • compiles to documentation

markdown rose to prevalence

  • translates to HTML
  • clear, concise
    • at the cost of being somewhat restrictive
  • almost as readable as the rendered HTML
  • can convert between MANY different formats

markdown + pandoc leads to Knitr

In Use

Knitr runs in a separate environment

If you use markdown

  • Can include things like videos, etc. using HTML

Can also use LaTex, and others as input

The rest of the talk is in reference to using Knitr with Rmarkdown

Like markdown syntax except

Shortcut for codeblock in RStudio

  • ctrl + alt + i
  • cmd + alt + i

Neat Tips:

As code gets longer

  • make separate R files
  • source them in the Knitr.

Use things like stargazer package

can run a chunk

  • By highlighting and right-clicking to run as with other code in RStudio

can comment out

  • using HTML commenting
<!-- any markdown including mix of text and codeblocks here -->


  • cache option, if true
    • doesn't re-eval itself if previous chunks has updated
    • re-evals if changed within chunk

Name code blocks

  • can see in RStudio and use as navigation
  • can reference code blocks elsewhere in document

Can write knitr hooks

  • extra customization!


  • stackoverflow

    • learned about caching
  • For taking code from an explanation to use

    • option of knitr('filename', tangle=TRUE)
  • slides, with ioslides_presentation

What are some workflows that include Knitr when doing analysis in R?

  • whole workflow in Rmd
  • do in RStudio first, then go to Rmarkdown
  • sourcing a lot of R code chunks
    • source one big file that sources other things

Is there a way to do interactive Rmarkdown editing online?


Good Practices and reports generation

Why use Rmarkdown for report generation?

  • Excel/Word with macros == sucked
    • brittle, any small change = broken
  • Rmarkdown is cleaner
  • get to leverage the power of R
    • for analysis
    • for plots, graphs, etc
  • easy to re-run anlysis and keep report in sync
  • easier to publish online


  • Difficult to have exact control over styles
  • Lack of good .ppt generation for some audiences


  • load.R, cleanup.R, figs.R, main.R
  • source main that loads all
  • custom ggplot theme
  • customization can mean getting into css/html

Other things!

Beamer for slides, and there are cool beamer themes

Brew is another way to do things like we saw with Knitr

  • Takes more up front set up though

you can make knitr to mark new page \newpage

  • \newpage is a feature of Tex

Reproducible Research

  • "places knitr in context with functions and packages it plays nice with, and some alternatives"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment