R Markdown provides a flexible environment for writing reports and other documents. The beauty of R Markdown is that it allows you integrate R code and output with written analysis in a single document. For the computational portions of the last three problem sets, you will submit documents generated by R Markdown.
Simplify your workflow
Communicate your results
Automate repetitive tasks
To open a new R Markdown file, click File
then New File
then R Markdown...
in R Studio. At this point, a dialog box will appear. By default, Document
and HTML
should be pre-selected in the dialog box. Type in a title and your name then click OK
.
The new R Markdown document comes pre-loaded with examples. To compare the code to the document it produces, click the Knit
button.
An R Markdown file is the source code for the document that you will generate. You can execute the source code by clicking the Kint
button in R Studio. When you do this, R will start generating a document. The document will show up in a new window once R is done “knitting” the file.
R Markdown files are also known by their file extension: .Rmd
.
At the top of your .Rmd
file, you should see some text sandwiched between triple dashes:
---
title: "A pithy title"
author: "Your Name"
date: "10/20/2019"
output: html_document
---
This text is called the YAML header. It contains basic information about the document that you will create.
The argument output: html_document
tells R Markdown to produce an HTML document. You can also tell R Markdown to produce other kinds of documents. For example, you will learn how to use R Markdown to produce MS Word documents further below.
Next, you should see some R code sandwiched by a pair of back ticks:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
The pair of backticks defines a code chunk. A code chunk is a minature R script that lives inside the .Rmd
file.
The code chunk above is named setup
. Generally speaking, you don’t have to name a code chunk, but names can be useful for helping your future self understand what your code chunk seeks to accomplish.
The argument include=FALSE
is a code chunk option that instructs R to execute the code chunk without showing the code and the results of the code in the document. This option overides the default seting, which is to show the code and the results in the document.
The code knitr::opts_chunk$set(echo = TRUE)
specifies default options for all code chunks.
You can add a new code chunk by clicking the Insert
button then R
, which results in
```{r}
```
You can write your R code of inside the code chunk:
```{r}
# Here is a comment inside of a code chunk
library(pacman)
p_load(tidyverse)
random_data <- tibble(
x = rnorm(1000, mean = 5, sd = 2),
y = 10 + x + rnorm(1000, mean = 0, sd = 1)
)
mean(random_data$y)
```
Note: In .Rmd
files, R only executes code typed inside code chunks. If you write code outside the backticks that define a code chunk, then R will render the code as text when you Knit
the file.
You can write text outside the YAML header and the code chunks.
Most of you write with “what you see is what you get” text editors, like MS Word. When you type text in MS Word, you see the final output as you write it. Markdown is different. To style your text in markdown, you have to use something called markdown syntax.
Markdown syntax:
text here
produces text here**text here**
produces text here*text here*
produces text here***text here***
produces text here$Y_i = \beta_0 + \beta_1 X_i + u_i$
produces \(Y_i = \beta_0 + \beta_1 X_i + u_i\)These are just a few examples things you can do with markdown syntax. R Studio’s R Markdown Cheat Sheet has a more complete list of examples.
HTML documents are the default R Markdown output. For more details on the functionality of output: html_document
, check out chapter 3.1 of R Markdown: The Definitive Guide.
You can also generate MS Word documents. As with the HTML documents, making Word documents in R Markdown can save you the hassle of copying and pasting your R code and output into an external document. The difference, however, is that you can edit the Word documents that R Markdown produces in the MS Word editor. This option gives you the best of both worlds. For more information, check out chapter 3.4 of R Markdown: The Definitive Guide.
Before knitting your .Rmd
file, save it to your EC 320 folder. When you click Knit
, the output document will show up in the same folder as your .Rmd
file.