p.s. Allison Horst’s R illustrations are amaaaazing and I will be using them throughout.
2021/03/09
p.s. Allison Horst’s R illustrations are amaaaazing and I will be using them throughout.
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
Simple syntax that allows you to add tags to plain text to format it
Originally designed to be HTML replacement
“Minimalist writing system”
Lifehacker: What is markdown and why is it better for my to do lists and notes?
✏️ Very useful for writing summary reports, articles, etc.
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:
YAML: “YAML Ain’t Markup Language”
Basic:
title: "Untitled" author: "Thea Knowles" date: '2018-02-18' output: word_document
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"
Even more options…
Different options for:
Note: We will just learn about HTML output today
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.
include is set to FALSE, indicating that we don’t want the contents of this chunk included in the outputFirst, insert a new R chunk by
Alt + Cmd/Ctrl + i ORCode >> Insert Chunk from the R Studio menu```{r}
x <- 10
```
```{r my-chunk}
x <- 10
```
⚠️ Chunk labels CANNOT contain spaces, underscores, or special characters, but CAN contain hyphens.
```{r my-chunk, echo = FALSE}
x <- 10
```
In this example, we set the echo option:
echo = TRUE: show the code in the rendered Rmd documentecho = FALSE: don’t show the code in the output, but DO run it in the backgroundLet’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)
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This chunk provides the following information for “knitting” the document:
echo = TRUE for all chunks (you can still set echo = FALSE in individual chunks later if you want)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
Tables written like this:
First Header | Second Header ------------- | ------------- Content Cell | Content Cell Content Cell | Content Cell
See remedy and beautifyR R packages/RStudio addins for dealing with markdown tables
Data frames can be turned into tables without manual modification
kable and flextable R packagesYou can refer to variables stored in R in your code using the syntax: `r [r code here!]`
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
In the Rmarkdown document you created from the default template…
Delete everything after Line 12 (after the setup code chunk)
Add a new header
Add some plain text
Add some bold text
Add a code chunk that includes x <- 5 and x
Include a line that says:
The value of x is `r x`
Compile! (“Knit”): Cmd/Ctrl + K or with Knit button.
helper.R file in my RMarkdown document and use code chunks to polish figures/tableshelper.RFirst: make sure simulated_vot_data.csv is in your project folder (same level as the .RProject). Then:
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!
helper.R in your RMarkdown documentOpen 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!
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)`.
Add another code chunk:
```{r vot-distribution}
hist(vot$VOT)
```
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
Build your skill set to tailor to your goals: Some guiding suggestions based on my own experience
ioslides_presentation output, but xaringan is an extremely powerful & popular tool for RMarkdown presentationshere are some of the additional “extras” we talked about