December 13, 2016
All source code at https://github.com/rladies/rtp_20161213_rmarkdown
Demo materials at http://bit.ly/rladies-rmd-demo (will come up later)
R Packages that we will use today: dplyr, ggplot2, rmarkdown, knir, DT, flexdashboards
packages <- c("dplyr", "ggplot2", "rmarkdown", "knir",
"DT", "flexdashboards")
install.packages(packages)
lapply(packages, library, character.only = TRUE)
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 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…
If you have not yet done so:
install.packages("rmarkdown")
library(rmarkdown)
(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.
install.packages("ggplot2")
install.packages("dplyr")
DEMO
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
Code chunks
Inline code
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 fileecho = 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 filewarning = FALSE: warnings do not appear in the finished filefig.cap = "..." adds a caption to graphical resultsSee R Markdown Reference Guide for all options.
cache = TRUE
Tricky, but can be useful!
References: - http://yihui.name/knitr/options/ - http://yihui.name/knitr/demo/cache/
Code results can be inserted directly into the text of a .Rmd file by enclosing the code with ` r`
DEMO
Not super easy to customize, but there are some options that are easy to use and look good
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(mtcars[, 1:5], extensions = 'ColReorder',
options = list(colReorder = TRUE,
pageLength = 5))
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
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)
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.packages("flexdashboard")
library(flexdashboard)
In RStudio:
File -> New File -> R Markdown… -> From Template -> Flex Dashboard
DEMO
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
beamer_presentation - PDF presentations with beamerioslides_presentation - HTML presentations with ioslidesslidy_presentation - HTML presentations with slidyrevealjs::revealjs_presentation - HTML presentations with reveal.jsWebsites: rmarkdown::render_site
Interactive documents: with htmlwidgets or Shiny