Outline

Why RMarkdown?

1) RMarkdown Basics

RStudio -> File -> New File ->R Markdown (keep defaults, add title)

Prompt to start new RMarkdown document.

Prompt to start new RMarkdown document.

Header for RMarkdown document.

Header for RMarkdown document.

Text and code chunk.

Text and code chunk.

Markdown:

  1. system for writing web pages, marking up text (like in an email).
  2. Marked up text gets converted to html, with marks replaced by proper html code.
  • Markdown Basics:
    • a bolded statement
    • an italicized statement
    • code-type font: code-type font
    • this is a bulleted list
  1. A numbered
  2. List
  3. Can be made
  4. Like this
1. A numbered
1. List
1. Can be made
1. Like this

HTML Headers:

#Header 1
##Header 2
###Header 3

Header 1

Header 2

Header 3

Delete default RMarkdown text (except global options)

# Add Some Notes
## Reflections on Markdown So Far

Markdown is **super** awesome, I'm *not* even joking. 

Why I love `RMarkdown`:

1. It makes things reproducible
2. Easy to collaborate
3. It's fun!

Add Some Notes

Reflections on Markdown So Far

Markdown is super awesome, I’m not even joking.

Why I love RMarkdown:

  1. It makes things reproducible
  2. Easy to collaborate
  3. It’s fun!

Generate basic RMarkdown document.

CHALLENGE 1: Make a new RMarkdown document. Delete all of the R code chunks and write a bit of Markdown (some sections, italicized/bold text, itemized list). Knit to HTML

More Markdown (if time allows)

  • add a hyperlink: [text to show](http://the-web-page.com)
  • include an image: ![image caption](http://url/for/file)
  • Sub-script (F2): F~2~ and super-script ( F2): F^2^.
  • LaTeX code: (\(E=mc^2\)) $E=mc^2$ or formulas: \[y = \mu + \sum_{i=1}^p \beta_i x_i + \epsilon\] ($$y = \mu + \sum_{i=1}^p \beta_i x_i + \epsilon$$).

2)R code chunks

A main code chunk:

Code chunk example

Code chunk example

Place R code between the sets of ticks. You may also give each code chunk a name, which can help you find errors:

Named code chunk example

Named code chunk example

Can create a new code chunk manually (with backticks) or short-cut: CTRL+ALT+i.

[Importing surveys dataset into RMarkdown and creating a visualization with R code chunk options.]

library(ggplot2)
library(dplyr)
download.file("http://kbroman.org/datacarp/portal_clean.csv", "portal_clean.csv")
surveys <- read.csv("portal_clean.csv")
ggplot(surveys, aes(x=weight, y=hindfoot_length)) +
   geom_point(aes(color=species_id, shape=sex)) +
  facet_wrap(~year)

CHALLENGE 2: Add code chunks to load ggplot2 package, read in portal data, create a plot.

3) Code chunk options

Example of R code chunk options specifyin that the code should not be output in the final report (echo=FALSE) and any messaged should also be suppressed (message=FALSE)

Example of R code chunk options specifyin that the code should not be output in the final report (echo=FALSE) and any messaged should also be suppressed (message=FALSE)

Global code chunk example

Global code chunk example

Plot with chunk options example

Plot with chunk options example

Species weight and height by year

Species weight and height by year

CHALLENGE 3: Use chunk options to control the size of a figure and hide the code.

4) Inline R Code

CHALLENGE 4: Try out in-line code in R
surveys <- read.csv("portal_clean.csv")
There are `r nrow(surveys)` observations in the *surveys* dataset.

There are 30652 observations in the surveys dataset.

5) Other Output formats

(May take some additional installation on your computer)

In addition to HTML documents, RMarkdown can also knit to PDF or Word documents.

Other output format options

Other output format options

Resources