December 13, 2016

Welcome to R-Ladies RTP!

Materials

Recording and reporting your analysis

Options

  • Writing code in the Console – error prone (especially typo prone!) and hard to keep track of

  • R Script + LaTeX/Word – can be reproducible with careful organization but still error prone

  • R Markdown – code, output, prose all in one place and fully reproducible, can call R scripts as well

R Markdown

  • R Markdown is an authoring format that enables easy creation of dynamic documents, presentations, and reports from R

  • R Markdown documents are fully reproducible, they can be automatically regenerated whenever underlying R code or data changes

  • File -> New File -> R Markdown…

  • Important features:
    • An (optional) YAML header surrounded by —s
    • R code chunks surrounded by ```s
    • Text mixed with simple markdown formatting
      • Human readable! (in RStudio -> Help -> Markdown Quick Reference)
      • Limited, so not too time consuming to master
    • Self contained workspace

R Markdown installation

If you have not yet done so:

install.packages("rmarkdown")
library(rmarkdown)

Reports

How it works

(in case you're interested…)

  • When you knit, R Markdown feeds the .Rmd file to knitr, which executes all of the code chunks and creates a new markdown (.md) document which includes the code and its output.

  • The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.

Demo

  • If you have not yet done so:
    • Load the following packages:
    install.packages("ggplot2")
    install.packages("dplyr")
  • Open demo-template.Rmd in RStudio and Knit



DEMO

Output options

  • HTML - web ready

  • PDF - requires TeX to be installed

  • Word - can be useful if collaborators are Word users, but their changes cannot be easily integrated

R code in R Markdown documents

Two options:

  • Code chunks

  • Inline code

Code chunks

  • Inserting a new code chunk:
    • keyboard shortcut Ctrl + Alt + I (OS X: Cmd + Option + I)
    • the Insert command in the editor toolbar (see other languages as well)
    • typing the chunk delimiters (```{r} to start, ``` to end)
  • When file is rendered R Markdown will run each code chunk and embed the results beneath the code chunk in your final report

Chunk options

Chunk output can be customized with knitr options, arguments set in the {} of a chunk header.

Examples:

  • include = FALSE: code runs and results be used by other chunks, but doesn't appear in finished file
  • echo = FALSE: prevents code, but not the results from appearing in the finished file (useful way to embed figures)
  • message = FALSE: messages do not appear in the finished file
  • warning = FALSE: warnings do not appear in the finished file
  • fig.cap = "..." adds a caption to graphical results

See R Markdown Reference Guide for all options.

Caching

Inline code

Code results can be inserted directly into the text of a .Rmd file by enclosing the code with ` r`



DEMO

Tables

Tables

Not super easy to customize, but there are some options that are easy to use and look good

knitr::kable

knitr::kable(mtcars[, 1:5])
mpg cyl disp hp drat
Mazda RX4 21.0 6 160.0 110 3.90
Mazda RX4 Wag 21.0 6 160.0 110 3.90
Datsun 710 22.8 4 108.0 93 3.85
Hornet 4 Drive 21.4 6 258.0 110 3.08
Hornet Sportabout 18.7 8 360.0 175 3.15
Valiant 18.1 6 225.0 105 2.76
Duster 360 14.3 8 360.0 245 3.21
Merc 240D 24.4 4 146.7 62 3.69
Merc 230 22.8 4 140.8 95 3.92
Merc 280 19.2 6 167.6 123 3.92
Merc 280C 17.8 6 167.6 123 3.92
Merc 450SE 16.4 8 275.8 180 3.07
Merc 450SL 17.3 8 275.8 180 3.07
Merc 450SLC 15.2 8 275.8 180 3.07
Cadillac Fleetwood 10.4 8 472.0 205 2.93
Lincoln Continental 10.4 8 460.0 215 3.00
Chrysler Imperial 14.7 8 440.0 230 3.23
Fiat 128 32.4 4 78.7 66 4.08
Honda Civic 30.4 4 75.7 52 4.93
Toyota Corolla 33.9 4 71.1 65 4.22
Toyota Corona 21.5 4 120.1 97 3.70
Dodge Challenger 15.5 8 318.0 150 2.76
AMC Javelin 15.2 8 304.0 150 3.15
Camaro Z28 13.3 8 350.0 245 3.73
Pontiac Firebird 19.2 8 400.0 175 3.08
Fiat X1-9 27.3 4 79.0 66 4.08
Porsche 914-2 26.0 4 120.3 91 4.43
Lotus Europa 30.4 4 95.1 113 3.77
Ford Pantera L 15.8 8 351.0 264 4.22
Ferrari Dino 19.7 6 145.0 175 3.62
Maserati Bora 15.0 8 301.0 335 3.54
Volvo 142E 21.4 4 121.0 109 4.11

DT::datatable

DT::datatable(mtcars[, 1:5], extensions = 'ColReorder', 
              options = list(colReorder = TRUE, 
                             pageLength = 5))

Notebooks

Notebook

  • When you open an R Markdown file in the RStudio IDE, it becomes a notebook interface for R.

  • You can run each code chunk by clicking the green arrow icon. RStudio executes the code and display the results inline with your file.

  • Other chunk actions…



DEMO

Version control with notebooks

  • One of the major advantages of R Notebooks compared to other notebook systems is that they are plain-text files and therefore work well with version control

  • If using version control, check both the .Rmd and .nb.html files into version control so that both your source code and output are available to collaborators

  • If you want each collaborator to work with their own private copies of the output check only the .Rmd file (with a .gitignore that excludes the .nb.html)

More on Notebooks

Dashboards

flexdashboards

  • Dashboards based on R Markdown: easy to compose and publish

  • Intelligent resizing: to fill browser and display on mobile devices

  • Support a wide variety of components: htmlwidgets, base, lattice, and grid graphics, tabular data, gauges and value boxes, and text

  • Shiny compatible!

Install

install.packages("flexdashboard")
library(flexdashboard)

Get started

In RStudio:

File -> New File -> R Markdown… -> From Template -> Flex Dashboard



DEMO

Layouts

Debugging

Debugging tips for R Markdown

  • Line number in error message – note that this will refer to the beginning of the R chunk, hence shorter R chunks make debugging easier

  • knitr::knit_exit(): stop knitting HERE

More with R Markdown

More with R Markdown

  • Slide presentations
    • beamer_presentation - PDF presentations with beamer
    • ioslides_presentation - HTML presentations with ioslides
    • slidy_presentation - HTML presentations with slidy
    • revealjs::revealjs_presentation - HTML presentations with reveal.js
  • Websites: rmarkdown::render_site

  • Interactive documents: with htmlwidgets or Shiny