In this lab, you will be introduced to RMarkdown using RStudio. A great feature of RStudio is that you can create so-called data-driven documents that combine text and analysis into a “reproducible” document. In other words, it allows you to write all of the text, syntax for the analysis, etc. and keep it in a document. This way you are not bouncing between programs and everything is reproducible. This is ideal if you are using a publicly available data set or for making your workflow reproducible.

Why are you learning this? We will be using RMarkdown templates for your homework assignments in this course. When you get to the homework assignments with templates, you will be working with RMarkdown. So, we want to start learning it early!

Also, this is a skill you should develop as you expand your data analysis tool kit.


Data-Driven Documents

What are Data-Driven Documents?

Data-driven documents are formats that combine text and analysis (i.e. data and code). By combining everything in a single file, we promote transparency and reproducibility. For any given table, figure, or model in the document, you should be able to easily discern how it was created, from what data, and what analysis was used.

We will use the R Markdown format.

How do Data-Driven Documents Work?

All of the document formats build from a simple text formatting convention called markdown.

To create an R Markdown document, you need three things:

  1. A header to specify the document type
  2. Some text (formatted in markdown)
  3. Some code (inside a “code chunk”)


Let’s see how it works!


RMarkdown in RStudio

First, you need to install the rmarkdown and knitr packages using:

install.packages( "rmarkdown" )
install.packages( "knitr" )


Now, let’s take a look at how it works. Open a new RMarkdown file using the drop-down menu as shown below:



Then, use the options to pick the type of file you want to create.


Afterwards, click on the knit function in the .Rmd pane:



You did it!


A Closer Look

Knitting R Markdown Files

Code is placed inside of “chunks” in the documents:

When you “knit” a file R Studio will run all of code, embed the output into your document, and then convert the file to whichever type you have specified in the file header.


Output Types

You can select from many different document types, including HTML pages, Microsoft word, presentation formats, or dashboards.


Check out these examples:

R Markdown Formats
R Markdown Gallery


HTML Pages

---
output: html_document
---


Dashboards

---
output: flexdashboard::flex_dashboard:
---

[ dashboard example ] [ source code ] [ blog about the tracker ]


PDFs

---
output: pdf_document
---


A note on PDFs

If you would like to knit to PDF you need one additional program. TeX creates publication-quality PDF files. The open-source version is called MiKTeX download page.

If you have problems, you can find some nice tutorials like this one: https://www.reed.edu/data-at-reed/software/R/r_studio_pc.html


Working with Markdown

Markdown is a set of simple conventions for formatting text in R Markdown (RMD) files.

It makes it easy to create professional documents with minimal effort.

Here are the basic formatting rules for Markdown:

Headers

# Heading One (h1)

## Heading Two (h2)

### Heading Three (h3)

#### Heading Four (h4)

##### Heading Five (h5)

###### Heading Six (h6)


Text Style

With Markdown, it is possible to emphasize words by making them *italicized*, using *astericks* or _underscores_, or making them **bold**, using **double astericks** or __double underscores__. 

Of course, you can combine those two formats, with both _**bold and italicized**_ text, using any combination of the above syntax. 

You can also add a strikethrough to text using a ~~double tilde~~.

With Markdown, it is possible to emphasize words by making them italicized, using astericks or underscores, or making them bold, using double astericks or double underscores.

Of course, you can combine those two formats, with both bold and italicized text, using any combination of the above syntax.

You can also add a strikethrough to text using a double tilde.


Lists

Unordered

* First item
* Second item
* Third item
    * First nested item
    * Second nested item
  • First item
  • Second item
  • Third item
    • First nested item
    • Second nested item

Ordered

1. First item
2. Second item
3. Third item
    1. First nested item
    2. Second nested item
  1. First item
  2. Second item
  3. Third item
    1. First nested item
    2. Second nested item


Images

Insert images in a similar way, but add an exclamation mark in front of square brackets [ ], and the image file name goes in the parentheses ( ).

![alt_text_here](image_file.png)

The alt text appears when the image cannot be located, or is read by devices for the blind when the mouse hovers over the image. It


Or you can link directly to an image online using the URL address of the image:

![](https://www.rodaw.com/wp-content/uploads/2017/02/Mark-Down-MonsterDogLampShade-1.jpg)


Tables

| Title 1          | Title 2          | Title 3         | Title 4         |
|------------------|------------------|-----------------|-----------------|
| First entry      | Second entry     | Third entry     | Fourth entry    |
| Fifth entry      | Sixth entry      | Seventh entry   | Eight entry     |
| Ninth entry      | Tenth entry      | Eleventh entry  | Twelfth entry   |
| Thirteenth entry | Fourteenth entry | Fifteenth entry | Sixteenth entry |
Title 1 Title 2 Title 3 Title 4
First entry Second entry Third entry Fourth entry
Fifth entry Sixth entry Seventh entry Eight entry
Ninth entry Tenth entry Eleventh entry Twelfth entry
Thirteenth entry Fourteenth entry Fifteenth entry Sixteenth entry


Summary

Why are you learning this? We will be using RMarkdown templates for your homework assignments in this course. When you get to the homework assignments with templates, you will be working with RMarkdown. So, we want to start learning it early!


Github Issues

Let’s navigate to the HELP! page for this course to see how Github Issues works.


Questions?


Please report any needed corrections to the Issues page. Thanks!


Back to SAND main page