2021/03/09

Overview

Learn how to use R Markdown to combine writing and code

📝 Create new R project

📝 Create, edit, and compile .Rmd file

📝 Create & edit helper.R file to feed to your .Rmd

Recap: Console and script panes in RStudio


  • Console: Run code, see print outs, see warnings, messages, and errors


  • Source: Run code from a script. Multiple scripts can be open at once.

What is “Markdown?”

What is R Markdown?

https://rmarkdown.rstudio.com/

  • Integrate R code directly into your writing using basic Markdown syntax
  • Reference management integration
  • Reproducibility
  • Accessible learning curve


✏️ Very useful for writing summary reports, articles, etc.

R Markdown

We’re going to make this today!

Part 1

The basics

📝 Exercise 1

Create a new R Project

  1. Make a new directory for this workshop
  2. Put the workshop contents in the same directory

  • You can restore where you left off by opening the .RProj file again later.
  • More on RProjects

📝 Exercise 2

Create a new R Markdown document

  1. Create a new R Markdown file
  2. Title it something useful
  3. Save it in the project folder you just made
  4. Compile! (“Knit”): Cmd/Ctrl + K or with Knit button

Essential parts of any R Markdown document

Essential parts of any R Markdown document

YAML

YAML (rhymes with camel): The header that tells R Markdown how to generate your document. Indentation and spacing are very important.

  • Permits the following to happen when you knit:

    • .Rmd -> knitr -> .md -> Pandoc -> output
    • Output can be .docx, .html, .pdf, and many others
  • YAML: “YAML Ain’t Markup Language”

YAML

Basic:

title: "Untitled"
author: "Thea Knowles"
date: '2018-02-18'
output: word_document

YAML

More options…

title: "Changes in voice acoustics along a speech rate continuum
        in Parkinson's disease"
author: "Thea Knowles, Scott G. Adams, Mandar Jog"
date: Last updated `r Sys.Date()`
output:
  bookdown::word_document2:
    reference_docx: "../rmd_templates/custom_reference.docx"
  redoc::redoc:
    highlight_outputs: FALSE
    margins: 1
    line_numbers: FALSE
bibliography: references.bib
csl: "csl_files/apa7.csl"

YAML

Even more options…

  • templates for Word, PDF, etc.
  • bibliography file
  • csl (references style guide)
  • css (supreme customization!)
  • journal articles, slides, websites

Different options for:

Note: We will just learn about HTML output today

Essential parts of any R Markdown document

Code chunks

  • Chunks are sections that will include R code. By setting defaults at the beginning of your document, you can specify what you want most of your chunks to do.

  • In each chunk, you can specify options in the form tag=value in the chunk header.

    • For example, in the following, the tag include is set to FALSE, indicating that we don’t want the contents of this chunk included in the output

Code chunks

  • Let’s say we want to include a code chunk that assigns x the value of 10.
  • Here are some ways we can do that.

Code chunks

First, insert a new R chunk by

  • Typing Alt + Cmd/Ctrl + i OR
  • Clicking Code >> Insert Chunk from the R Studio menu

Code chunks

Bare R code chunk (no labels or options)

```{r}
x <- 10
```

Code chunks

Give it a label (“my-chunk”)

```{r my-chunk}
x <- 10
```

⚠️ Chunk labels CANNOT contain spaces, underscores, or special characters, but CAN contain hyphens.

Code chunks

Tell RMarkdown what to do with it (Set chunk options)

```{r my-chunk, echo = FALSE}
x <- 10
```

In this example, we set the echo option:

  • echo = TRUE: show the code in the rendered Rmd document
  • echo = FALSE: don’t show the code in the output, but DO run it in the background

Code chunks: More options

Code chunks: More options

Code chunks: Special setup code chunk

Let’s say you want to set default behavior for all of your code chunks. You can do this using the knitr package in a special chunk at the beginning of your document.

When you create a new Rmarkdown document from the default template, this is included for you:

```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
```

Code chunks: Special setup code chunk

```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
```

This chunk provides the following information for “knitting” the document:

  • setup: the name of the chunk You shouldn’t have two chunks with the same name, unless they are unnamed (in which case they just get numbered automatically during the knit process)
  • include = false: the chunk will not be included in the output after knitting.
  • knitr::opts_chunk$set(echo = TRUE): set echo = TRUE for all chunks (you can still set echo = FALSE in individual chunks later if you want)

Essential parts of any R Markdown document

R Markdown

Text in Markdown syntax

Markdown

Markdown: set of conventions for editing plain text.

Write as you normally would in a text editor or word processor, but you signal text formatting with certain characters (next slide).

Markdown (which is distinguished from markUP language) is designed to be

  • easy to read
  • easy to write
  • easy to learn

Markdown syntax

Markdown syntax

Tables written like this:

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell
  • See Tables Generator website

  • See remedy and beautifyR R packages/RStudio addins for dealing with markdown tables

  • Data frames can be turned into tables without manual modification

    • See kable and flextable R packages

Inline R code

You can refer to variables stored in R in your code using the syntax: `r [r code here!]`

Example:

Earlier in your code you have assigned x <- 5 in an R chunk

x <- 5

Typing

The value of x is `r x`

will print

The value of x is 5

📝 Exercise 3

Customize

In the Rmarkdown document you created from the default template…

  1. Delete everything after Line 12 (after the setup code chunk)

  2. Add a new header

  3. Add some plain text

  4. Add some bold text

  5. Add a code chunk that includes x <- 5 and x

  6. Include a line that says:

    The value of x is `r x`

  7. Compile! (“Knit”): Cmd/Ctrl + K or with Knit button.

Part 2

Scaling up to real-world use

My habits

  • do my data cleaning/exploration in a separate .R script (“helper.R”)
  • source my helper.R file in my RMarkdown document and use code chunks to polish figures/tables
  • include in-line R code that refers back to R objects made in helper.R

📝 Exercise 4

Create helper.R

First: make sure simulated_vot_data.csv is in your project folder (same level as the .RProject). Then:

  1. Create a new .R script
  2. Add the following code
  3. Save it as helper.R in your project folder (same level as your .RProject for now)

Try to run the code in the R console.

🐛 We can pause to diagnose any errors people run into!

📝 Exercise 5

Source & use the code in helper.R in your RMarkdown document

Open your .Rmd file and insert a new code chunk below the setup chunk

```{r source-helper}
    source("helper.R")
    
    min(vot$VOT)
    max(vot$VOT)
```

Knit the .Rmd document. What do you see?

🐛 We can pause to diagnose any errors people run into!

📝 Exercise 6

Add in-line R code

Below your R chunk, add the following lines:

The mean VOT is `r vot_m`.

The mean VOT is `r min(vot$VOT)` and the max VOT is `r max(vot$VOT)`.

📝 Exercise 7

Add a plot

Add another code chunk:

```{r vot-distribution}
    hist(vot$VOT)
```

Exponentially useful

The ability to refer to your code in your document becomes EXTREMELY helpful once you start creating multiple figures, reporting summary variables and tables of statistics


🗺 Tour of some of the ways I use RMarkdown

  • Manuscripts
  • Dissertation
  • Presentations like this one that needs to show/demonstrate code
  • Summary documents to supervisor/colleagues/stats consults
  • “Trapper-keeper” for large projects
  • My websites

Manuscript draft

Dissertation record keeping

Build your skills gradually

Build your skill set to tailor to your goals: Some guiding suggestions based on my own experience

  • Start with using R Markdown to tell the story of your data to you, to your supervisor (summary reports)
  • If dissertating - start early! Use RMarkdown as a way to keep track of your data. Compile frequently and read up on project management
  • Write an article in RMarkdown & learn how to use templates, reference management integration, and .RData files
  • Present your data using RMarkdown
    • This presentation uses the ioslides_presentation output, but xaringan is an extremely powerful & popular tool for RMarkdown presentations

R Studio Visual R Markdown 🔥

More resources

More resources

And even more resources

here are some of the additional “extras” we talked about

  • More on snippets: Autocomplete text shortcuts for common strings of text.
  • RData vs. RDS: Load in your environment or specific R objects for faster rendering
  • tjmisc::ggpreview(): preview your plots before knitting
  • RMarkdown: notebooks vs. documents: Notebooks allow a special execution mode of R Markdown that allows you to see the results of your chunks in the Source pane as you go. All .Rmd files can be rendered as notebooks and vice versa.

Thank you!!!