+ - 0:00:00
Notes for current slide
Notes for next slide

Reproducible Research with RMarkdown

Jennifer Thompson, MPH

Osaka City University, 2018-06-05

1 / 61

Summary & Goals

RMarkdown allows you to do modern reproducible research with powerful features, particularly in the HTML format.

2 / 61

Summary & Goals

RMarkdown allows you to do modern reproducible research with powerful features, particularly in the HTML format.

After today, you will:

  • Understand Markdown syntax
  • Know how to create reproducible research documents in PDF and HTML format using RMarkdown
  • Be aware of additional packages that can help your reports be more informative
  • Have a list of resources to help you learn even more going forward
2 / 61

What is Reproducible Research?

You perform analysis in 2014; in 2016, the primary investigator is ready to write the paper. She asks you for new figures, and has another year of data she wants to add.

3 / 61

What is Reproducible Research?

You perform analysis in 2014; in 2016, the primary investigator is ready to write the paper. She asks you for new figures, and has another year of data she wants to add.


You submit a paper, and it takes several months to get feedback from reviewers. When you receive it, they have requested additional analyses and changes to figures.

3 / 61

What is Reproducible Research?

You perform analysis in 2014; in 2016, the primary investigator is ready to write the paper. She asks you for new figures, and has another year of data she wants to add.


You submit a paper, and it takes several months to get feedback from reviewers. When you receive it, they have requested additional analyses and changes to figures.


You got a promotion 🎊, and need to send your code to the coworker who will take over your projects.

3 / 61

Scenario 1

  • Your code wasn't saved, or was saved in a mystery location
  • You used several scripts
  • You saved your code, but

    • The output was copied & pasted into Word
    • The figures were done separately, in Excel or SPSS (which are very fine tools!)

    Result: 💔

4 / 61

Scenario 1

  • Your code wasn't saved, or was saved in a mystery location
  • You used several scripts
  • You saved your code, but

    • The output was copied & pasted into Word
    • The figures were done separately, in Excel or SPSS (which are very fine tools!)

    Result: 💔

Scenario 2

  • Your code was saved in a sensible, accessible location, in a single file
  • Original tables and figures were generated by the same script that did the calculations
  • Old and new data are in similar formats

    Result: 😊

4 / 61

How can we achieve 😊?

Using reproducible research strategies, we can create and store all code and data needed to recreate our analysis results.

5 / 61

How can we achieve 😊?

Using reproducible research strategies, we can create and store all code and data needed to recreate our analysis results.

This might involve:

  • No point and click: Code scripts for everything!
  • Sensible file storage structures
  • Analysis, table generation, and figure generation all done together
  • Code is well documented

We'll discuss each of these in more detail!

5 / 61

Point & Click vs Scripted Code

Point & Click

(Excel, SPSS, or R without saving it)

  • Relies on memory and your notes to know for certain what you did
  • No way to know for certain what went wrong if you get different numbers
  • Worst of all: If anything changes, you have to do it all over again

    Result: 💔

6 / 61

Point & Click vs Scripted Code

Point & Click

(Excel, SPSS, or R without saving it)

  • Relies on memory and your notes to know for certain what you did
  • No way to know for certain what went wrong if you get different numbers
  • Worst of all: If anything changes, you have to do it all over again

    Result: 💔

Scripted, Saved Code

  • Every step you took is saved in a script (or several)
  • Tables and figures can be regenerated using the same script and data
  • Updating with new data is usually straightforward

    Result: 😊

6 / 61

Sensible File Structures

Key points: Make it easy to both keep yourself organized and allow someone else to use your code without changing all the file paths or searching for the right data.

😢: When Project 2 needs C:\My Documents\Jennifer\Project1Data\spreadsheet1.xlsx

7 / 61

Sensible File Structures

Key points: Make it easy to both keep yourself organized and allow someone else to use your code without changing all the file paths or searching for the right data.

😢: When Project 2 needs C:\My Documents\Jennifer\Project1Data\spreadsheet1.xlsx

Example File Structure

ParentStudy \
analysisdata \
Project1 \
analysisdata \
proj1_analysis.Rmd
Project2 \
helperscripts \
proj2_analysis.Rmd
eg, "OurRCT"
Analysis datasets from OurRCT
Subfolder, Project 1
Additional data only for Project 1
Analysis script for Project 1
Subfolder, Project 2
R scripts only used in Project 2
Analysis script for Project 2

For someone else to take over Project 2, everything they need will be in the Project2 folder and analysisdata/.

7 / 61

Sensible File Structures

Key points: Make it easy to both keep yourself organized and allow someone else to use your code without changing all the file paths or searching for the right data.

😢: When Project 2 needs C:\My Documents\Jennifer\Project1Data\spreadsheet1.xlsx

Example File Structure

ParentStudy \
analysisdata \
Project1 \
analysisdata \
proj1_analysis.Rmd
Project2 \
helperscripts \
proj2_analysis.Rmd
eg, "OurRCT"
Analysis datasets from OurRCT
Subfolder, Project 1
Additional data only for Project 1
Analysis script for Project 1
Subfolder, Project 2
R scripts only used in Project 2
Analysis script for Project 2

For someone else to take over Project 2, everything they need will be in the Project2 folder and analysisdata/.

For more, see Jenny Bryan's excellent blog post on project-oriented workflow.

7 / 61

Analysis, tables, figures done together

Ideal

All data management, analysis, and figure generation done in a single script

8 / 61

Analysis, tables, figures done together

Ideal

All data management, analysis, and figure generation done in a single script

  • Easier to know which code produced which result, because it's all together
  • No confusing "v2" with "v3", or "submitted" with "resubmitted"
8 / 61

Analysis, tables, figures done together

Ideal

All data management, analysis, and figure generation done in a single script

  • Easier to know which code produced which result, because it's all together
  • No confusing "v2" with "v3", or "submitted" with "resubmitted"

Reasonable exceptions (not exhaustive)

  • Large projects with lots of data management required (this is the reason for the analysisdata/ directory on the last slide!)
  • Separate scripts of helper functions (eg, yesterday's data monitoring tutorial) - often easier to maintain and debug these in a separate script

    If you have many helper functions, you could even consider creating an R package for your project!

8 / 61

Code Documentation

Literate Programming

Have you ever...

  • Opened an R script you wrote six months ago and realized you have no idea what it does?
  • Taken over a project from someone else and spent days trying to figure out what their script was doing?
9 / 61

Code Documentation

Literate Programming

Have you ever...

  • Opened an R script you wrote six months ago and realized you have no idea what it does?
  • Taken over a project from someone else and spent days trying to figure out what their script was doing?

"You are always working with at least one collaborator: Future you." - Hadley Wickham

9 / 61

Code Documentation

Literate Programming

Literate programming is a technique for mixing written text and chunks of code together.

This can allow for easier documentation of code, and/or the production of a full written report that incorporates the code and results used to create it.

10 / 61

Code Documentation

Literate Programming

Literate programming is a technique for mixing written text and chunks of code together.

This can allow for easier documentation of code, and/or the production of a full written report that incorporates the code and results used to create it.

Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to humans what we want the computer to do.

— Donald E. Knuth, Literate Programming, 1984

10 / 61

How are these related?

There are several tools in R which allow us to accomplish both of these last two goals.

11 / 61

How are these related?

There are several tools in R which allow us to accomplish both of these last two goals.

  • Sweave: The original method for literate programming in R, which allows the creation of complete research documents in PDF format by combining R and LaTeX
11 / 61

How are these related?

There are several tools in R which allow us to accomplish both of these last two goals.

  • Sweave: The original method for literate programming in R, which allows the creation of complete research documents in PDF format by combining R and LaTeX
  • knitr: Newer package for literate programming which builds on Sweave, but has more flexibility
11 / 61

How are these related?

There are several tools in R which allow us to accomplish both of these last two goals.

  • Sweave: The original method for literate programming in R, which allows the creation of complete research documents in PDF format by combining R and LaTeX
  • knitr: Newer package for literate programming which builds on Sweave, but has more flexibility
  • RMarkdown: Powerful package for literate programming, reproducible analysis, and document generation, which can (often working with knitr):
    • Combine R code and Markdown syntax
    • Produce documents in not only PDF format, but Microsoft Word and various types of HTML documents (like these slides!)
    • In HTML format, incorporate "extras" like interactive graphics
11 / 61

What is Markdown?

Markdown is a lightweight markup language that allows you to quickly write and format text, which is then converted to different types of output.

12 / 61

What is Markdown?

Markdown is a lightweight markup language that allows you to quickly write and format text, which is then converted to different types of output.

Advantages

  • Lightweight syntax: Less to learn/type than LaTeX or HTML
  • Can do the vast majority of what you need with less effort
  • Syntax resembles the finished product
12 / 61

What is Markdown?

Markdown is a lightweight markup language that allows you to quickly write and format text, which is then converted to different types of output.

Advantages

  • Lightweight syntax: Less to learn/type than LaTeX or HTML
  • Can do the vast majority of what you need with less effort
  • Syntax resembles the finished product

Disadvantages

  • Less control, fewer options than LaTeX or HTML

    (...BUT you can also incorporate actual LaTeX or HTML if you need more control!)

12 / 61

What is Markdown?

Markdown is a lightweight markup language that allows you to quickly write and format text, which is then converted to different types of output.

Advantages

  • Lightweight syntax: Less to learn/type than LaTeX or HTML
  • Can do the vast majority of what you need with less effort
  • Syntax resembles the finished product

Disadvantages

  • Less control, fewer options than LaTeX or HTML

    (...BUT you can also incorporate actual LaTeX or HTML if you need more control!)

Here's a tutorial!
12 / 61

Markdown Syntax

We want to write a paragraph where we bold some words, put some in italics and some in verbatim, include a

Main Header

And a Subheader

  • bulleted
  • list
    • with a
    • sublist,

and an

  1. enumerated list
  2. with a random number inserted, like 0.8

and a quote, like this

13 / 61

LaTeX (Sweave/knitr Rnw file) Version

We want to write a paragraph where we \textbf{bold} some words, put some in
\emph{italics} and some in \texttt{verbatim}, include a
\section{Main Header}
\subsection{And Section Header}
\begin{itemize}
\item bulleted
\item list
\begin{itemize}
\item with a
\item sublist,
\end{itemize}
\end{itemize}
and an
\begin{enumerate}
\item enumerated list
\item with a random number like \Sexpr{round(runif(n = 1), 2)}
\end{enumerate}
\quote and a quote, like this
14 / 61

Markdown Version

We want to write a paragraph where we **bold** some words, put some in *italics*
and some in `verbatim`, include a
## Main Header
### And a Subheader
- bulleted
- list
- with a
- sublist,
and an
1. enumerated list
1. with a random number inserted, like `r round(runif(n = 1), 2)`
> and a quote, like this
15 / 61

Your Turn!

  1. Make sure you have the RMarkdown package installed and have run library(rmarkdown) in your R session
  2. Open a new text file, write some text describing a project you're working on, and format it however you like using Markdown syntax
  3. Save your file with the extension .md (for Markdown)
  4. Use RMarkdown's render() function to create an HTML file:

rmarkdown::render("myfile.md")

For more help with syntax, also see:

https://daringfireball.net/projects/markdown/syntax

16 / 61

R + Markdown

Input

RMarkdown files combine R code chunks with text formatted in Markdown, and are always saved with the .Rmd extension (eg, analysis.Rmd).

You can also include "inline" R code that is not in a specific chunk.

For example, here is text before an R code chunk where I generate a
random sample from a normal distribution.
```r
my_sample <- rnorm(n = 100)
```
Then later, I want to refer to the mean, which is `r
mean(my_sample)`.
17 / 61

RMarkdown Output

Some options:

18 / 61

RMarkdown Output

Some options:

18 / 61

RMarkdown Output

Some options:

You can create multiple types of output with the same .Rmd file:

rmarkdown::render(input = "analysis.Rmd", output = "pdf_document") rmarkdown::render(input = "analysis.Rmd", output = "word_document")

18 / 61

RMarkdown Document Format

  1. YAML
19 / 61

RMarkdown Document Format

  1. YAML

  2. Markdown text

  3. R code
19 / 61

RMarkdown Document Format

  1. YAML

  2. Markdown text

  3. R code

  4. Repeat 2 & 3 as needed

Sweave users: RMarkdown and knitr documents work very similarly to Sweave, but with slightly different syntax and options for code chunks.

19 / 61

YAML

(Yet Another Markup Language)

YAML is the "metadata" - the data about your document - that goes at the top of your file and tells RMarkdown things like titles and document options.

---
title: "My Analysis Report"
author: "Jennifer Thompson, MPH"
date: "2018-06-05"
output: pdf_document
---
20 / 61

YAML

(Yet Another Markup Language)

YAML is the "metadata" - the data about your document - that goes at the top of your file and tells RMarkdown things like titles and document options.

---
title: "My Analysis Report"
author: "Jennifer Thompson, MPH"
date: "2018-06-05"
output: pdf_document
---

This is the minimum YAML most RMarkdown documents will have, but you can specify many other options depending on the output format you use. Examples to come!

20 / 61

The output line sets the default output format for this document.

If we specify a format here, we don't have to specify it in the render() function.

Or, we can specify the default format here, but use render() to create output of a different format - maybe we usually want to create a PDF, but at the end we also create a Word document to make writing a manuscript easier.

Your Turn!

  1. Open the test Markdown file you just created, and add some YAML to the top of the document.
    • You can specify whatever output format you like: pdf_document, word_document, html_document (or try them all!).
  2. Render it again, using rmarkdown::render("myfile.md").
  3. Check to make sure you got the file output you requested.

🚨 NOTE 🚨: Even though we are not using LaTeX for formatting, you will need to have a TeX installation on your computer if you want to create a PDF.

21 / 61

Your Turn!

  1. Open the test Markdown file you just created, and add some YAML to the top of the document.
    • You can specify whatever output format you like: pdf_document, word_document, html_document (or try them all!).
  2. Render it again, using rmarkdown::render("myfile.md").
  3. Check to make sure you got the file output you requested.

🚨 NOTE 🚨: Even though we are not using LaTeX for formatting, you will need to have a TeX installation on your computer if you want to create a PDF.

  1. BONUS: Leave the file YAML the same, but use rmarkdown::render()'s output_format option to specify a different output format than the one in your YAML.
21 / 61

Your Turn!

  1. Open the test Markdown file you just created, and add some YAML to the top of the document.
    • You can specify whatever output format you like: pdf_document, word_document, html_document (or try them all!).
  2. Render it again, using rmarkdown::render("myfile.md").
  3. Check to make sure you got the file output you requested.

🚨 NOTE 🚨: Even though we are not using LaTeX for formatting, you will need to have a TeX installation on your computer if you want to create a PDF.

  1. BONUS: Leave the file YAML the same, but use rmarkdown::render()'s output_format option to specify a different output format than the one in your YAML.

rmarkdown::render("myfile.md", output_format = "word_document")

21 / 61

Combining R + Markdown

RMarkdown documents are Markdown-formatted text, with "chunks" of R code placed where you need them. You designate an R code chunk using backticks ` and braces { }:

```{r chunk_name, [options]}

## R code here

```

22 / 61

Combining R + Markdown

RMarkdown documents are Markdown-formatted text, with "chunks" of R code placed where you need them. You designate an R code chunk using backticks ` and braces { }:

```{r chunk_name, [options]}

## R code here

```

options include various things that you want to control for that particular chunk, including things like

  • Figure height and width
  • Whether to echo (show) or evaluate (run) that code chunk in the final document
  • Whether to show warnings and messages

These are passed to knitr; you can see a full list of knitr options at yihui.name/knitr/options/.

22 / 61

Your Turn!

  1. Add a chunk of R code to your Markdown file that creates a basic scatterplot: plot(1:10, 1:10).
  2. Re-save the file with the .Rmd extension (because now it includes R and Markdown code).
  3. Render the file.
23 / 61

Your Turn!

  1. Add a chunk of R code to your Markdown file that creates a basic scatterplot: plot(1:10, 1:10).
  2. Re-save the file with the .Rmd extension (because now it includes R and Markdown code).
  3. Render the file.

  4. BONUS: Change some options: Hide the code itself using echo = FALSE, and change the figure height and width using fig.height and fig.width (remember that these are in inches).

Remember, you can find other options at yihui.name/knitr/options.

23 / 61

Your Turn!

  1. Add a chunk of R code to your Markdown file that creates a basic scatterplot: plot(1:10, 1:10).
  2. Re-save the file with the .Rmd extension (because now it includes R and Markdown code).
  3. Render the file.

  4. BONUS: Change some options: Hide the code itself using echo = FALSE, and change the figure height and width using fig.height and fig.width (remember that these are in inches).

Remember, you can find other options at yihui.name/knitr/options.

Next: Add another chunk that displays the first six rows of the airquality dataset, using the results = "asis" option.

23 / 61

Tables

The results = "asis" option renders the output of our R code chunks "as is", meaning, as it is formatted within the R chunk. In this example, that doesn't work well!

knitr::kable() can help us with that. Try updating your R code chunk:

Before:

head(iris[, 1:2])

Sepal.Length Sepal.Width 1 5.1 3.5 2 4.9 3.0 3 4.7 3.2 4 4.6 3.1 5 5.0 3.6 6 5.4 3.9

After:

library(knitr)
kable(head(iris[, 1:2]))
Sepal.Length Sepal.Width
5.1 3.5
4.9 3.0
4.7 3.2
4.6 3.1
5.0 3.6
5.4 3.9
24 / 61

Simplicity vs Control

knitr::kable() is great for simple tables; it purposefully does not offer many features, to keep use simple.

25 / 61

Simplicity vs Control

knitr::kable() is great for simple tables; it purposefully does not offer many features, to keep use simple.

This is also true of Markdown: It is simple on purpose, to keep from complicating your life and your code!

Simplicity saves time and allows us to focus on content rather than formatting.

25 / 61

Simplicity vs Control

knitr::kable() is great for simple tables; it purposefully does not offer many features, to keep use simple.

This is also true of Markdown: It is simple on purpose, to keep from complicating your life and your code!

Simplicity saves time and allows us to focus on content rather than formatting.

Markdown: 90% of the results from 10% of the effort.
25 / 61

Simplicity vs Control

knitr::kable() is great for simple tables; it purposefully does not offer many features, to keep use simple.

This is also true of Markdown: It is simple on purpose, to keep from complicating your life and your code!

Simplicity saves time and allows us to focus on content rather than formatting.

Markdown: 90% of the results from 10% of the effort.

But...

Sometimes we want something more complicated. We have to choose:

25 / 61

Simplicity vs Control

knitr::kable() is great for simple tables; it purposefully does not offer many features, to keep use simple.

This is also true of Markdown: It is simple on purpose, to keep from complicating your life and your code!

Simplicity saves time and allows us to focus on content rather than formatting.

Markdown: 90% of the results from 10% of the effort.

But...

Sometimes we want something more complicated. We have to choose:

Simplicity

Control & Features

25 / 61

Simplicity vs Control: Example

The kableExtra package is an excellent add-on package for adding complexity to tables in either LaTeX or HTML. But: Some of its features are only available in HTML documents.

kable(head(iris), format = "...") %>%
group_rows("Group 1", 1, 3) %>%
group_rows("Group 2", 4, 6) %>%
add_header_above(c("Sepal" = 2, "Petal" = 2)) %>%
## HTML only
# scroll_box(width = "250px", height = "200px") %>%
# kable_styling(bootstrap_options = "hover")

`kable(..., format = "latex")`

Sepal
Petal
Species
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
5.0 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
4.9 3.1 1.5 0.1 setosa
5.4 3.7 1.5 0.2 setosa
4.8 3.4 1.6 0.2 setosa
4.8 3.0 1.4 0.1 setosa
4.3 3.0 1.1 0.1 setosa
5.8 4.0 1.2 0.2 setosa
5.7 4.4 1.5 0.4 setosa
5.4 3.9 1.3 0.4 setosa
5.1 3.5 1.4 0.3 setosa
5.7 3.8 1.7 0.3 setosa
5.1 3.8 1.5 0.3 setosa
5.4 3.4 1.7 0.2 setosa
5.1 3.7 1.5 0.4 setosa
4.6 3.6 1.0 0.2 setosa
5.1 3.3 1.7 0.5 setosa
4.8 3.4 1.9 0.2 setosa
5.0 3.0 1.6 0.2 setosa
5.0 3.4 1.6 0.4 setosa
5.2 3.5 1.5 0.2 setosa
5.2 3.4 1.4 0.2 setosa
4.7 3.2 1.6 0.2 setosa
4.8 3.1 1.6 0.2 setosa
5.4 3.4 1.5 0.4 setosa
5.2 4.1 1.5 0.1 setosa
5.5 4.2 1.4 0.2 setosa
4.9 3.1 1.5 0.2 setosa
5.0 3.2 1.2 0.2 setosa
5.5 3.5 1.3 0.2 setosa
4.9 3.6 1.4 0.1 setosa
4.4 3.0 1.3 0.2 setosa
5.1 3.4 1.5 0.2 setosa
5.0 3.5 1.3 0.3 setosa
4.5 2.3 1.3 0.3 setosa
4.4 3.2 1.3 0.2 setosa
5.0 3.5 1.6 0.6 setosa
5.1 3.8 1.9 0.4 setosa
4.8 3.0 1.4 0.3 setosa
5.1 3.8 1.6 0.2 setosa
4.6 3.2 1.4 0.2 setosa
5.3 3.7 1.5 0.2 setosa
5.0 3.3 1.4 0.2 setosa
7.0 3.2 4.7 1.4 versicolor
6.4 3.2 4.5 1.5 versicolor
6.9 3.1 4.9 1.5 versicolor
5.5 2.3 4.0 1.3 versicolor
6.5 2.8 4.6 1.5 versicolor
5.7 2.8 4.5 1.3 versicolor
6.3 3.3 4.7 1.6 versicolor
4.9 2.4 3.3 1.0 versicolor
6.6 2.9 4.6 1.3 versicolor
5.2 2.7 3.9 1.4 versicolor
5.0 2.0 3.5 1.0 versicolor
5.9 3.0 4.2 1.5 versicolor
6.0 2.2 4.0 1.0 versicolor
6.1 2.9 4.7 1.4 versicolor
5.6 2.9 3.6 1.3 versicolor
6.7 3.1 4.4 1.4 versicolor
5.6 3.0 4.5 1.5 versicolor
5.8 2.7 4.1 1.0 versicolor
6.2 2.2 4.5 1.5 versicolor
5.6 2.5 3.9 1.1 versicolor
5.9 3.2 4.8 1.8 versicolor
6.1 2.8 4.0 1.3 versicolor
6.3 2.5 4.9 1.5 versicolor
6.1 2.8 4.7 1.2 versicolor
6.4 2.9 4.3 1.3 versicolor
6.6 3.0 4.4 1.4 versicolor
6.8 2.8 4.8 1.4 versicolor
6.7 3.0 5.0 1.7 versicolor
6.0 2.9 4.5 1.5 versicolor
5.7 2.6 3.5 1.0 versicolor
5.5 2.4 3.8 1.1 versicolor
5.5 2.4 3.7 1.0 versicolor
5.8 2.7 3.9 1.2 versicolor
6.0 2.7 5.1 1.6 versicolor
5.4 3.0 4.5 1.5 versicolor
6.0 3.4 4.5 1.6 versicolor
6.7 3.1 4.7 1.5 versicolor
6.3 2.3 4.4 1.3 versicolor
5.6 3.0 4.1 1.3 versicolor
5.5 2.5 4.0 1.3 versicolor
5.5 2.6 4.4 1.2 versicolor
6.1 3.0 4.6 1.4 versicolor
5.8 2.6 4.0 1.2 versicolor
5.0 2.3 3.3 1.0 versicolor
5.6 2.7 4.2 1.3 versicolor
5.7 3.0 4.2 1.2 versicolor
5.7 2.9 4.2 1.3 versicolor
6.2 2.9 4.3 1.3 versicolor
5.1 2.5 3.0 1.1 versicolor
5.7 2.8 4.1 1.3 versicolor
6.3 3.3 6.0 2.5 virginica
5.8 2.7 5.1 1.9 virginica
7.1 3.0 5.9 2.1 virginica
6.3 2.9 5.6 1.8 virginica
6.5 3.0 5.8 2.2 virginica
7.6 3.0 6.6 2.1 virginica
4.9 2.5 4.5 1.7 virginica
7.3 2.9 6.3 1.8 virginica
6.7 2.5 5.8 1.8 virginica
7.2 3.6 6.1 2.5 virginica
6.5 3.2 5.1 2.0 virginica
6.4 2.7 5.3 1.9 virginica
6.8 3.0 5.5 2.1 virginica
5.7 2.5 5.0 2.0 virginica
5.8 2.8 5.1 2.4 virginica
6.4 3.2 5.3 2.3 virginica
6.5 3.0 5.5 1.8 virginica
7.7 3.8 6.7 2.2 virginica
7.7 2.6 6.9 2.3 virginica
6.0 2.2 5.0 1.5 virginica
6.9 3.2 5.7 2.3 virginica
5.6 2.8 4.9 2.0 virginica
7.7 2.8 6.7 2.0 virginica
6.3 2.7 4.9 1.8 virginica
6.7 3.3 5.7 2.1 virginica
7.2 3.2 6.0 1.8 virginica
6.2 2.8 4.8 1.8 virginica
6.1 3.0 4.9 1.8 virginica
6.4 2.8 5.6 2.1 virginica
7.2 3.0 5.8 1.6 virginica
7.4 2.8 6.1 1.9 virginica
7.9 3.8 6.4 2.0 virginica
6.4 2.8 5.6 2.2 virginica
6.3 2.8 5.1 1.5 virginica
6.1 2.6 5.6 1.4 virginica
7.7 3.0 6.1 2.3 virginica
6.3 3.4 5.6 2.4 virginica
6.4 3.1 5.5 1.8 virginica
6.0 3.0 4.8 1.8 virginica
6.9 3.1 5.4 2.1 virginica
6.7 3.1 5.6 2.4 virginica
6.9 3.1 5.1 2.3 virginica
5.8 2.7 5.1 1.9 virginica
6.8 3.2 5.9 2.3 virginica
6.7 3.3 5.7 2.5 virginica
6.7 3.0 5.2 2.3 virginica
6.3 2.5 5.0 1.9 virginica
6.5 3.0 5.2 2.0 virginica
6.2 3.4 5.4 2.3 virginica
5.9 3.0 5.1 1.8 virginica
26 / 61

Simplicity vs Control: Example

The kableExtra package is an excellent add-on package for adding complexity to tables in either LaTeX or HTML. But: Some of its features are only available in HTML documents.

kable(head(iris), format = "...") %>%
group_rows("Group 1", 1, 3) %>%
group_rows("Group 2", 4, 6) %>%
add_header_above(c("Sepal" = 2, "Petal" = 2)) %>%
## HTML only
# scroll_box(width = "250px", height = "200px") %>%
# kable_styling(bootstrap_options = "hover")

`kable(..., format = "latex")`

Sepal
Petal
Species
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
5.0 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
4.9 3.1 1.5 0.1 setosa
5.4 3.7 1.5 0.2 setosa
4.8 3.4 1.6 0.2 setosa
4.8 3.0 1.4 0.1 setosa
4.3 3.0 1.1 0.1 setosa
5.8 4.0 1.2 0.2 setosa
5.7 4.4 1.5 0.4 setosa
5.4 3.9 1.3 0.4 setosa
5.1 3.5 1.4 0.3 setosa
5.7 3.8 1.7 0.3 setosa
5.1 3.8 1.5 0.3 setosa
5.4 3.4 1.7 0.2 setosa
5.1 3.7 1.5 0.4 setosa
4.6 3.6 1.0 0.2 setosa
5.1 3.3 1.7 0.5 setosa
4.8 3.4 1.9 0.2 setosa
5.0 3.0 1.6 0.2 setosa
5.0 3.4 1.6 0.4 setosa
5.2 3.5 1.5 0.2 setosa
5.2 3.4 1.4 0.2 setosa
4.7 3.2 1.6 0.2 setosa
4.8 3.1 1.6 0.2 setosa
5.4 3.4 1.5 0.4 setosa
5.2 4.1 1.5 0.1 setosa
5.5 4.2 1.4 0.2 setosa
4.9 3.1 1.5 0.2 setosa
5.0 3.2 1.2 0.2 setosa
5.5 3.5 1.3 0.2 setosa
4.9 3.6 1.4 0.1 setosa
4.4 3.0 1.3 0.2 setosa
5.1 3.4 1.5 0.2 setosa
5.0 3.5 1.3 0.3 setosa
4.5 2.3 1.3 0.3 setosa
4.4 3.2 1.3 0.2 setosa
5.0 3.5 1.6 0.6 setosa
5.1 3.8 1.9 0.4 setosa
4.8 3.0 1.4 0.3 setosa
5.1 3.8 1.6 0.2 setosa
4.6 3.2 1.4 0.2 setosa
5.3 3.7 1.5 0.2 setosa
5.0 3.3 1.4 0.2 setosa
7.0 3.2 4.7 1.4 versicolor
6.4 3.2 4.5 1.5 versicolor
6.9 3.1 4.9 1.5 versicolor
5.5 2.3 4.0 1.3 versicolor
6.5 2.8 4.6 1.5 versicolor
5.7 2.8 4.5 1.3 versicolor
6.3 3.3 4.7 1.6 versicolor
4.9 2.4 3.3 1.0 versicolor
6.6 2.9 4.6 1.3 versicolor
5.2 2.7 3.9 1.4 versicolor
5.0 2.0 3.5 1.0 versicolor
5.9 3.0 4.2 1.5 versicolor
6.0 2.2 4.0 1.0 versicolor
6.1 2.9 4.7 1.4 versicolor
5.6 2.9 3.6 1.3 versicolor
6.7 3.1 4.4 1.4 versicolor
5.6 3.0 4.5 1.5 versicolor
5.8 2.7 4.1 1.0 versicolor
6.2 2.2 4.5 1.5 versicolor
5.6 2.5 3.9 1.1 versicolor
5.9 3.2 4.8 1.8 versicolor
6.1 2.8 4.0 1.3 versicolor
6.3 2.5 4.9 1.5 versicolor
6.1 2.8 4.7 1.2 versicolor
6.4 2.9 4.3 1.3 versicolor
6.6 3.0 4.4 1.4 versicolor
6.8 2.8 4.8 1.4 versicolor
6.7 3.0 5.0 1.7 versicolor
6.0 2.9 4.5 1.5 versicolor
5.7 2.6 3.5 1.0 versicolor
5.5 2.4 3.8 1.1 versicolor
5.5 2.4 3.7 1.0 versicolor
5.8 2.7 3.9 1.2 versicolor
6.0 2.7 5.1 1.6 versicolor
5.4 3.0 4.5 1.5 versicolor
6.0 3.4 4.5 1.6 versicolor
6.7 3.1 4.7 1.5 versicolor
6.3 2.3 4.4 1.3 versicolor
5.6 3.0 4.1 1.3 versicolor
5.5 2.5 4.0 1.3 versicolor
5.5 2.6 4.4 1.2 versicolor
6.1 3.0 4.6 1.4 versicolor
5.8 2.6 4.0 1.2 versicolor
5.0 2.3 3.3 1.0 versicolor
5.6 2.7 4.2 1.3 versicolor
5.7 3.0 4.2 1.2 versicolor
5.7 2.9 4.2 1.3 versicolor
6.2 2.9 4.3 1.3 versicolor
5.1 2.5 3.0 1.1 versicolor
5.7 2.8 4.1 1.3 versicolor
6.3 3.3 6.0 2.5 virginica
5.8 2.7 5.1 1.9 virginica
7.1 3.0 5.9 2.1 virginica
6.3 2.9 5.6 1.8 virginica
6.5 3.0 5.8 2.2 virginica
7.6 3.0 6.6 2.1 virginica
4.9 2.5 4.5 1.7 virginica
7.3 2.9 6.3 1.8 virginica
6.7 2.5 5.8 1.8 virginica
7.2 3.6 6.1 2.5 virginica
6.5 3.2 5.1 2.0 virginica
6.4 2.7 5.3 1.9 virginica
6.8 3.0 5.5 2.1 virginica
5.7 2.5 5.0 2.0 virginica
5.8 2.8 5.1 2.4 virginica
6.4 3.2 5.3 2.3 virginica
6.5 3.0 5.5 1.8 virginica
7.7 3.8 6.7 2.2 virginica
7.7 2.6 6.9 2.3 virginica
6.0 2.2 5.0 1.5 virginica
6.9 3.2 5.7 2.3 virginica
5.6 2.8 4.9 2.0 virginica
7.7 2.8 6.7 2.0 virginica
6.3 2.7 4.9 1.8 virginica
6.7 3.3 5.7 2.1 virginica
7.2 3.2 6.0 1.8 virginica
6.2 2.8 4.8 1.8 virginica
6.1 3.0 4.9 1.8 virginica
6.4 2.8 5.6 2.1 virginica
7.2 3.0 5.8 1.6 virginica
7.4 2.8 6.1 1.9 virginica
7.9 3.8 6.4 2.0 virginica
6.4 2.8 5.6 2.2 virginica
6.3 2.8 5.1 1.5 virginica
6.1 2.6 5.6 1.4 virginica
7.7 3.0 6.1 2.3 virginica
6.3 3.4 5.6 2.4 virginica
6.4 3.1 5.5 1.8 virginica
6.0 3.0 4.8 1.8 virginica
6.9 3.1 5.4 2.1 virginica
6.7 3.1 5.6 2.4 virginica
6.9 3.1 5.1 2.3 virginica
5.8 2.7 5.1 1.9 virginica
6.8 3.2 5.9 2.3 virginica
6.7 3.3 5.7 2.5 virginica
6.7 3.0 5.2 2.3 virginica
6.3 2.5 5.0 1.9 virginica
6.5 3.0 5.2 2.0 virginica
6.2 3.4 5.4 2.3 virginica
5.9 3.0 5.1 1.8 virginica

Once we choose one format, our document will no longer render properly in another format.

26 / 61

Simplicity vs Control

Simplicity

  • Very little time spent formatting
  • One .Rmd file can create multiple types of output with ease
  • Limited in what you can display

Choose If

Little time, simple document, will create in multiple formats

27 / 61

Simplicity vs Control

Simplicity

  • Very little time spent formatting
  • One .Rmd file can create multiple types of output with ease
  • Limited in what you can display

Choose If

Little time, simple document, will create in multiple formats

Control/Features

  • Nicer-looking, more informative end product
  • Have to choose output format ahead of time
  • More time spent formatting

Choose If

Only need one version, as clear & informative as possible

27 / 61

Choosing a Format

So far, everything has translated equally well into a PDF, Word, or an HTML document. But when we choose control we give up that flexibility!

So how do you choose a format?

28 / 61

Choosing a Format

So far, everything has translated equally well into a PDF, Word, or an HTML document. But when we choose control we give up that flexibility!

So how do you choose a format?

Flow chart

28 / 61

The Power of HTML

  • Interactive graphics
  • Ability to include HTML, written by me (rare) or others (often)
  • Floating table of contents
  • Include code, without taking up visual space
  • Include details, without taking up visual space
  • Tabsets
  • Make it look pretty easily
29 / 61

Case Study with airquality

For the rest of our workshop, we'll be using the airquality dataset.

(Find out more about this dataset by typing ?airquality in your R session.)

head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
30 / 61

Case Study with airquality

For the rest of our workshop, we'll be using the airquality dataset.

(Find out more about this dataset by typing ?airquality in your R session.)

head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6

To Do List:

  1. Quickly summarize, explore dataset
  2. Use a linear model to predict ozone levels in New York
  3. Check model assumptions
  4. Produce nice table, publication-quality figure of model results
  5. Cite software, packages used
30 / 61

Your Turn!

Using RStudio

  1. File -> New File...
  2. Leave Document, HTML checked
  3. Add your title, save as airquality.Rmd

RStudio example

Without RStudio

Open a new file and insert this YAML, then save as airquality.Rmd:

---
title: "airquality"
author: "Your Name"
date: "2018-06-05"
output: html_document
---
31 / 61

Setup Chunk

In my first R code chunk, I typically include any knitr options I want to set throughout the document and load all the libraries I need for analysis.

```r
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(knitr)
library(plotly)
library(magrittr)
library(kableExtra)
```
32 / 61

Summarize & Explore Dataset

We want to explore our variables individually using histograms, and unadjusted relationships between each covariate and our outcome using scatterplots. While we could do this using static figures, interactive graphics could also be helpful.

We'll use the plotly package to do both! plotly supports many types of interactive graphics, with the ability to

  • Add information using tooltips
  • Combine plots using "subplots"
  • Zoom in to examine specific points
  • And more!

plotly is built on JavaScript; Carson Sievert maintains the R package which allows us to use it within R. Its excellent documentation is at plot.ly/r.

33 / 61

Using plotly for Histograms

Create a Single Plot

## -- Create a single histogram for Ozone --------------------------------------
ozone_hist <- plot_ly(x = airquality[, "Ozone"], type = "histogram")
ozone_hist
05010015005101520
34 / 61

Using plotly for Histograms

  1. Create a list of all our histograms
  2. Combine them using subplots in plotly
## Setup: Character vectors of variable names and hex colors
aq_vars <- c("Ozone", "Solar.R", "Wind", "Temp")
aq_cols <- c(
"Ozone" = "#88706c", "Solar.R" = "#84886c",
"Wind" = "#6c8488", "Temp" = "#706c88"
)
## Use lapply() to create a **list** of histograms
hist_list <- lapply(aq_vars, ## For each outcome/covariate,
FUN = function(xvar){
plot_ly( ## Create a plotly object...
x = airquality[, xvar], ## ...with [variable] on X axis
type = "histogram", ## Plot is a histogram
color = I(aq_cols[[xvar]]), ## Use this hex color for this variable
name = xvar ## name of "trace" (shows in legend)
) %>%
## Add variable name to X axis title
layout(xaxis = list(title = xvar))
}
)
35 / 61

Using plotly for Histograms

## Combine **all** the plots into one using plotly::subplot()
aq_histograms <- subplot(
hist_list, ## List of histograms we just created
nrows = 2, ## Rows our final object has
shareY = TRUE, ## Plots on same row should share a Y axis
titleX = TRUE, ## Keep individual X axis titles
margin = c(0.05, 0.05, 0.05, 0.1) ## Plot margins
)
36 / 61

Using plotly for Histograms

05010015001020304001002003005101520010203060708090
OzoneSolar.RWindTempOzoneSolar.RWindTemp
37 / 61

Your Turn!

  1. Add an introductory paragraph to your airquality.Rmd file with a brief description of the study.
  2. Using a Markdown header, add a section for data exploration.
  3. Add a plotly histogram of one of the variables in airquality to your document.
38 / 61

Bivariate Relationships

We can explore relationships between our outcome, Ozone, and the other covariates in our model by adding traces (layers) to our plotly plots.

ozone_scatter <- plot_ly(
## First trace: temperature vs ozone
x = airquality[, "Temp"], y = airquality[, "Ozone"],
type = "scatter", ## Scatterplot
name = "Temperature", ## Name for legend,
color = I(aq_cols[["Temp"]]), ## color (same as above)
alpha = 0.6, ## make points 60% opacity (1.0 = opaque)
marker = list(size = 15) ## make points larger for presentation
) %>%
## Add traces for Solar.R, Wind
## add_trace "inherits" anything we don't specify - use the same Y axis, eg
add_trace(
x = airquality[, "Solar.R"],
name = "Solar Radiation",
color = I(aq_cols[["Solar.R"]])
) %>%
add_trace(
x = airquality[, "Wind"], name = "Wind", color = I(aq_cols[["Wind"]])
) %>%
layout(yaxis = list(title = "Ozone"))
39 / 61

Bivariate Relationships

ozone_scatter
0100200300020406080100120140160180
TemperatureSolar RadiationWindOzone
40 / 61

Other Ways to Include Interactivity

There are several R packages for interactive graphics when using HTML output. They include:

The htmlwidgets site includes many other examples.

41 / 61

Inline R Code

Like Sweave, RMarkdown can include inline code: R code outside of code chunks. The syntax is slightly different:

Sweave

\Sexpr([expression])

RMarkdown

`r expression`

42 / 61

Inline R Code

Like Sweave, RMarkdown can include inline code: R code outside of code chunks. The syntax is slightly different:

Sweave

\Sexpr([expression])

RMarkdown

`r expression`

Your Turn!

To your airquality.Rmd file, add a paragraph using inline R code to describe how many observations will be excluded from our analysis due to missing data.

42 / 61

HTML Features

This section will demonstrate some of my favorite HTML features for statistical reports:

  • Hiding sections using details
  • Combining related information using tabset
  • Sparklines

Demo time! using other HTML reports

43 / 61

How To: Details

<details>
<summary>Optional summary of your details section</summary>
...
</details>
44 / 61

How To: Details

<details>
<summary>Optional summary of your details section</summary>
...
</details>

Collapsed

Opened

44 / 61

How To: Tabset

  • Helpful for multiple pieces of related information
    • Models of same outcome (eg, mortality) at different time points
    • Several types of results for same model

TS example

### Big Section{.tabset}
#### Subsection 1
Info, code chunks for subsection 1
#### Subsection 2
Info, code chunks for subsection 2
45 / 61

Your Turn!

Fit a linear model predicting ozone using solar radiation, wind, and temperature. Then,

  1. Visually check model assumptions using residual vs fitted and Q-Q plots
  2. Collapse the plots in a Details section, with each in a separate tabset
mymod <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality)
plot(resid(mymod) ~ fitted(mymod))

qqnorm(resid(mymod))

46 / 61

We Should Fix That

Let's log transform our outcome and re-check our assumptions:

mymod <- lm(log(Ozone) ~ Solar.R + Wind + Temp, data = airquality)
plot(resid(mymod) ~ fitted(mymod))

qqnorm(resid(mymod))

47 / 61

Tables with kableExtra

We can get a basic table of our coefficients with knitr::kable:

kable(summary(mymod)$coefficients, format = "html", digits = 2)
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.26 0.55 -0.47 0.64
Solar.R 0.00 0.00 4.52 0.00
Wind -0.06 0.02 -3.92 0.00
Temp 0.05 0.01 8.08 0.00

But we'd like a fancier one!

There are many, many packages for making tables, but kableExtra has lots of great features and excellent documentation. Note: kableExtra has plenty of LaTeX options as well as HTML!

48 / 61

Better Tables with kableExtra

kable(as.data.frame(summary(mymod)$coefficients), format = "html", digits = 2) %>%
add_header_above(c(" " = 1, "Estimation" = 2, "Testing" = 2)) %>%
group_rows("Predictors", 2, 4) %>%
row_spec(3:4, italic = TRUE) %>%
kable_styling(bootstrap_options = c("hover", "condensed"), full_width = FALSE) %>%
footnote(general = "Due to CSS, not all Bootstrap options work in these slides; move to demo")
Estimation
Testing
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.26 0.55 -0.47 0.64
Predictors
Solar.R 0.00 0.00 4.52 0.00
Wind -0.06 0.02 -3.92 0.00
Temp 0.05 0.01 8.08 0.00
Note:
Due to CSS, not all Bootstrap options work in these slides; move to demo
49 / 61

Your Turn!

Using the kableExtra documentation, format your own table of coefficients.

https://haozhu233.github.io/kableExtra

mod_coefs <- as.data.frame(summary(mymod)$coefficients)
kable(mod_coefs, ...) %>%
...
50 / 61

Your Turn!

Using the kableExtra documentation, format your own table of coefficients.

https://haozhu233.github.io/kableExtra

mod_coefs <- as.data.frame(summary(mymod)$coefficients)
kable(mod_coefs, ...) %>%
...

Other Packages for Creating Tables

And of course: If you know HTML or LaTeX, you can always use them directly.

50 / 61

Publication Figure

Our publication figures will likely need to be static, not interactive, so we'll create one here and use knitr chunk options to automatically save it. We want to look at the predicted log(ozone level) at each level of solar radiation, adjusted for wind speed and temperature.

pred_newdata <- data.frame(
Solar.R = seq(
min(airquality$Solar.R, na.rm = TRUE),
max(airquality$Solar.R, na.rm = TRUE),
length.out = 100
),
Wind = median(airquality$Wind, na.rm = TRUE),
Temp = median(airquality$Wind, na.rm = TRUE)
)
pred_vals <- predict(mymod, newdata = pred_newdata, se.fit = TRUE)
pred_newdata$log_ozone <- pred_vals$fit
pred_newdata$se_fit <- pred_vals$se.fit
pred_newdata$lcl_fit <- with(pred_newdata, log_ozone - qnorm(0.975)*se_fit)
pred_newdata$ucl_fit <- with(pred_newdata, log_ozone + qnorm(0.975)*se_fit)
51 / 61

Publication Figure

I create and print a ggplot2 plot as usual...

ggplot(data = pred_newdata, aes(x = Solar.R, y = log_ozone)) +
geom_ribbon(aes(ymin = lcl_fit, ymax = ucl_fit), alpha = 0.4) +
geom_line() +
labs(
title = "Adjusted log(Ozone) by Solar Radiation",
x = "Solar Radiation",
y = "Log(Ozone)",
caption = "\nOzone levels were log transformed to improve model fit."
) +
theme(plot.caption = element_text(face = "italic"))

...using these chunk options:

  • out.width = "75%": Display width = 75% of actual output width
  • fig.asp = 0.7: 70% as high as wide
  • fig.path = "pubfigures/": Store figure(s) in this subdirectory
  • dev = c("pdf", "png"): Create PDF, PNG versions of the figure
  • dpi = 300: Use 300 dots per inch
  • Chunk name: ozone_solarrad
52 / 61

Publication Figure

53 / 61

Publication Figure

Publication figures saved

53 / 61

Controlling Appearance with YAML

Our HTML document has some great features, but it's pretty plain. We can use RMarkdown's built-in themes to make it pretty.

---
title: "Test Markdown"
author: "Jennifer Thompson, MPH"
date: "2018-06-05"
output:
html_document:
theme: flatly
highlight: zenburn
---
54 / 61

Controlling Appearance with YAML

Our HTML document has some great features, but it's pretty plain. We can use RMarkdown's built-in themes to make it pretty.

---
title: "Test Markdown"
author: "Jennifer Thompson, MPH"
date: "2018-06-05"
output:
html_document:
theme: flatly
highlight: zenburn
---

Themes are based on CSS. If you know CSS, you can customize even more!

(If you don't, you can benefit from the hard work of others who do 😁)

54 / 61

Controlling Appearance with YAML

  • code_folding: hide, show
  • Table of Contents:
    • toc: include TOC, only at top by default
    • toc_float: make the TOC "float" on the side
    • toc_depth: how many section levels should your TOC show?

Show or hide code

TOC

55 / 61

Your Turn!

Experiment with the YAML in your airquality.Rmd file!

  • Change the theme and see what happens. Your options are:

    cerulean; journal; flatly; readable; spacelab; united; cosmo; lumen; paper; sandstone; simplex; yeti

  • Change the code highlight and see what happens. Your options are:

    tango; pygments; kate; monochrome; espresso; zenburn; haddock; textmate

  • Show or hide your code by default.

  • Format your Table of Contents using toc, toc_float, and toc_depth.
56 / 61

Software Citation

  • Reproducibility includes software!
  • Cite R, at minimum:
    • R.version$version.string = R version 3.4.3 (2017-11-30)
    • format(citation(), style = "textVersion") = R Core Team (2017). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/.
  • citation() can also help you cite specific packages:
    • format(citation("ggplot2"), style = "textVersion")
    • H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016.
57 / 61

Your Turn!

Add a citation of base R to the end of your report. (You can hide this in a Details section if you like.)

58 / 61

Your Turn!

Add a citation of base R to the end of your report. (You can hide this in a Details section if you like.)

Note: The devtools package has a nice session_info() function that I often include for completeness.

DT::datatable(devtools::session_info()$packages)
 
58 / 61

Other Capabilities

  • No matter which output format you choose, you can fall back on LaTeX or HTML itself if you need something very specific.
    • LaTeX Example: \newpage
    • HTML Example: <center>...</center>, <details>, <summary>
  • In either format, you can use LaTeX to insert math equations.
  • Sparklines! I love sparklines!
library(sparkline)
## -- Sparkline for ozone levels over time -------------------------------------
## Dataset is already sorted; thanks, R!
ozone_spark <- sparkline(
values = airquality$Ozone,
width = 200,
height = 75,
elementID = "spark_ozone"
)

Ozone levels over time:

59 / 61

Additional RMarkdown/knitr Resources

Please see earlier sections for links to resources on project-oriented workflows, tables, and options for interactive graphics.

60 / 61

Markdown Syntax Quick Reference

For more details, see daringfireball.net/projects/markdown/syntax

**bold**

*italic*

# Header 1

## Header 2 (etc)

[Text to link](URL)

![Image caption](image path)

> quote

- bulleted
- list
- sublist
- using 4 spaces
1. enumerated
1. list
1. again
1. with the spaces

Use `backticks` for fixed width text

Indent with 4 spaces for a code block

--- = horizontal rule

61 / 61

Summary & Goals

RMarkdown allows you to do modern reproducible research with powerful features, particularly in the HTML format.

2 / 61
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow