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

RMarkdown Basics

1 / 25

Recap

  • Reproducibility across all sciences is a huge issue!
  • We need to be able to reproduce scientific findings
    • This means the data & code from a given paper must be easy to access and human readable
  • When it comes to programming in R, using RMarkdown can help us make sure that other humans can understand our analyses

Artwork by Allison Horst

2 / 25

Anatomy of RNotebook

The anatomy of all .Rmd files (RNotebook or RMarkdown):

1. Document Information

2. Formatted Text               

3. Code Chunks                    

3 / 25

Anatomy of RNotebook

The anatomy of all .Rmd files (RNotebook or RMarkdown):

1. Document Information

2. Formatted Text               

3. Code Chunks                    

4 / 25

Document Information

  • Title
  • Subtitle
  • Date
  • Author
  • Output type

This is sometimes called a YAML header.

5 / 25

Document Information

  • Title
  • Subtitle
  • Date
  • Author
  • Output type

IMPORTANT: Do NOT change any of the tabs/indents in the YAML header. If you copy something from the internet, pay attention to this!

6 / 25

Anatomy of RNotebook

The anatomy of all .Rmd files (RNotebook or RMarkdown):

1. Document Information

2. Formatted Text               

3. Code Chunks                    

7 / 25

Formatted Text

You need to tell R how you want your text to be formatted:

  • Headers
  • Bolded text
  • Italicized text
  • Hyperlinks
  • Bullet/numbered lists
8 / 25

Headers

The number of # indicates what size and level your header should be.

9 / 25

Headers

10 / 25

Headers

11 / 25

Bold & Italics

Bold text

  • **bold text **
  • __bold text __ (2 underscores)

Italicized text

  • * italicized text *
  • _ italicized text _

To combine, pick * for one and _ for the other

  • **_combine_**
  • _**combine**_
  • *__combine__*
  • __*combine*__
12 / 25

Bullet Lists - Unordered

  • First line must end with a : (colon)
  • Must have an empty line
  • Must have a space after the bullet

Unformatted

Brazilian States:

- Rio Grande do Sul
- Parana
- Rio de Janeiro

Formatted

Brazilian States:

  • Rio Grande do Sul
  • Parana
  • Rio de Janeiro

Bullets can be - (dashes), + (plus), or * (asterisk), but all come out looking like what you see here.

14 / 25

Bullet Lists - Ordered

Same thing, but now with numbers

Unformatted

Brazilian States:

1. Rio Grande do Sul
2. Parana
3. Rio de Janeiro

Formatted

Brazilian States:

  1. Rio Grande do Sul
  2. Parana
  3. Rio de Janeiro
15 / 25

Bullet Lists - Nested

You can have organized, nested lists. Go to the next line, and press 2 spaces. Then put your new bullet symbol.

  • Do NOT press tab. For whatever reason, R doesn't like it for Markdown.
  • If you still are stuck, try 4 spaces -- that should work

Unformatted

Formatted

Brazilian States & Capitals:

  1. Rio Grande do Sul
    • Porto Alegre
      • This is the best!
  2. Parana
    • Curitiba
  3. Rio de Janeiro
    • Rio de Janeiro
16 / 25

Anatomy of RNotebook

The anatomy of all .Rmd files (RNotebook or RMarkdown):

1. Document Information

2. Formatted Text               

3. Code Chunks                    

17 / 25

Code Chunks

  • This is what makes RMarkdown so cool!
  • Type your code directly into a code chunk and work with it just like you would a .R script file
  • When you're done, click knit at the top to generate your pretty report
    • All code chunks will be executed (unless you say otherwise...see next lecture)
18 / 25

Code Chunks

Each code chunk:

  • Starts and ends with three backticks
    • if you don't have the ending 3, you're gonna have a bad time
  • Has {r} at top next to the first 3 backticks
  • Has gray background
  • Looks like normal code
  • Runs like normal code
19 / 25

Making Code Chunks

To make a code chunk:

  • Use the insert button
  • Manually type the backticks & {r}
  • Keyboard shortcuts
    • PCs: ctrl + alt + i
    • Macs: cmd + opt + i

20 / 25

A finished product

21 / 25

Code Chunks

All code will run in a code chunk

  • It's very literal!
  • We can't see midus or mean_age
    • These are stored as objects
    • If you want to see them, you need to tell R to show them to you
  • We needed to import midus
    • If it was in your Environment before knitting the file AND you did not import your data, it would fail
    • Whatever data you use, you need to import it!
22 / 25

Viewing data.frames

To help make data.frames readable for humans, use the kable() function

  • comes from the knitr package, although surprisingly, you don't need to manually load this one
23 / 25
24 / 25

Next up

Chaning parameters in the YAML header and in code chunks to make the output just right.

25 / 25

Recap

  • Reproducibility across all sciences is a huge issue!
  • We need to be able to reproduce scientific findings
    • This means the data & code from a given paper must be easy to access and human readable
  • When it comes to programming in R, using RMarkdown can help us make sure that other humans can understand our analyses

Artwork by Allison Horst

2 / 25
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