A An R Markdown Primer for Doing Homework
RStudio has included a method for making higher quality documents from a combination of R code and some basic markdown formatting, which is called R Markdown.
In order to create an R Markdown file, inside RStudio you click on File
, then New File
and R Markdown
. You will be prompted to enter a Title and an Author. For now, leave the Default Output Format as .html and leave the Document icon higlighted. When you have entered an acceptable title and author, click OK and a new tab in the Source pane should open up. (That is the top left pane in RStudio.)
You should see something that looks like this, with some more things underneath.
---
title: “Untitled”
author: “Darrin Speegle”
date: “1/20/2017”
output: html_document
---
This part of the code is the YAML (YAML Ain’t Markup Language). At this point, you will want to delete all of the text below the YAML, starting with
```{r setup, }
Let’s suppose that you are writing up solutions to a homework one. It would be a good idea to have a centered title that indicates which homewok this is, so you would change title: “Untitled” to title: “Homework 1”. The author of the document should be your name, and the date should be the date that the homework is due. Let’s suppose that the homework is due January 25, 2017. I would change the YAML, then, to look like this:
---
title: “Homework 1”
author: “Darrin Speegle”
date: “1/25/2017”
output: html_document
---
Just to see what that did, click on the Knit
button just above the Source pane. A window should pup up that says Homework 1 in bold, followed by your name and the date that you entered. If that didn’t happen, redo the above.
For the purposes of this primer, I am assuming that you are typing up solutions to a homework. Modify your file to make it look like this, and then Knit
again.
---
title: "Homework 1"
author: "Darrin Speegle"
date: "1/25/2017"
output: html_document
---
1. Consider the `mtcars` data set.
a. Which cars have 4 forward gears?
```{r}
mtcars[mtcars$gear == 4,]
```
The list of cars above are the cars with 4 forward gears.
b. Which cars have 4 forward gears and manual transmission?
Notes
- putting a 1. at the beginning of a line will start a numbered list.
- Putting mtcars in single quotes will make the background shaded, so that it is clear that it is an R object of some sort.
- The part beginning with ```{r} and ending with ``` is called an R chunk. You can create a new R chunk by using Command + Alt + I. When you knit your file, R Markdown puts the commands that used in a shaded box, to indicate that it is R code. Then, it also executes the R commands and shows the output.
- Text should not be inside of the R chunk. You should put your text either above or below the chunk.
- For the purposes of your homework, every problem needs to have some sort of explanation in words. Just giving the R code will not be sufficient. You will need to explain what it is you have done. (In the early homeworks, this can often be done quite quickly.)
- If you are using a package inside R Markdown, you must load the package inside your markdown file! I recommend loading all libraries at the top of the markdown file. For example, if you need to use the data set
Batting
in theLahman
package, I recommend starting your homework like this
---
title: "Homework 1"
author: "Darrin Speegle"
date: "1/25/2017"
output: html_document
---
```{r, echo = FALSE}
library(Lahman)
```
1. Consider the `mtcars` data set.
a. Which cars have 4 forward gears?
```{r}
mtcars[mtcars$gear == 4,]
```
The list of cars above are the cars with 4 forward gears.
b. Which cars have 4 forward gears and manual transmission?
In order to finish typing up the solutions to Homework 1, you should continue with your list of problems, and insert R chunks with the R code that you need plus some explanation in words for each problem.
When you are done, you should Knit
the document again, and a new .html file will appear. You should print out the pretty .html version (not the Markdown code) and hand that in. I will also want you to turn in your .Rmd file (the ugly Markdown code) separately. At this point, you are ready to go. The last thing I would say is that Markdown has a lot more typesetting that it can do than what I have indicated up to this point. There are many good resources on this, for example Grolemund and Wickham or Ismay.