I will provide a brief tutorial here on how to use R Markdown. This will provide you with the basics, which you will build upon as new problems arise (which you can troubleshoot using Google and the cheat sheet). Useful bits of information relevant to homework assignments will be noted with “HOMEWORK”.
It is always illustrative to compare what is in this Rmd file with what is produced when you knit together the document (instead of having me explain what every little thing in R Markdown does). Go ahead and click the Knit button at the top (or you can press CTRL+Shift+K for PC users or Cmd+Shift+K for Mac users). Note that when you knit an Rmd document, a corresponding preview of the html document is produced by RStudio. The actual html document is simultaneously produced in the same location as the Rmd file on your computer. If you open that html file, you will see that it matches exactly what is in the html previewer in RStudio. HOMEWORK: the html document is what you will submit on Canvas for your homework assignments.
The initial section of the R Markdown document set off by
--- is called the YAML. The YAML is where you can control
the details and format of your document. You can see that I added a
table of contents (toc: true) that floats
(toc_float: true). toc_depth: 2 means the
table of contents will display first and second-level headers marked off
with # and ## respectively. For example:
This subheader will appear in the TOC as you scroll down or if you
click “The YAML Header” in the TOC. HOMEWORK: it will
probably be most useful to label each homework question’s answer using a
header and the question number (e.g., # Question 1).
This is accomplished by just typing into the R Markdown document. HOMEWORK: you will use plain English text when answering homework questions that require it, or when I ask you to describe what you did (i.e., those answers that don’t require R code).
Note that not only can you display text as code, e.g.,
sqrt(), you can also run code in the middle of your text.
For example, the square root of 16 is 4. You can run R code in the
console by pressing CTRL+Enter for PC users (Cmd+Return for Mac users),
so you can see if the code is doing what you want it to. Try this out
when the cursor is on the in-line, square root 16 code above.
When typing up larger blocks of code to be embedded, you set up an R
code chunk. Code chunks are started using three
backticks and {r}, and ended with three backticks. As
before, you can run R code within R Markdown using CTRL+Enter or
Cmd+Return.
# Here is a code chunk.
head(mtcars) # look at the first six rows
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
You can see that all of my code, along with the code output, is embedded in the html output file.
There are also chunk options that allow you to not display the code,
not run the code, suppress warning messages, etc. All these options can
be found in the cheat sheet. Here, I will use eval = FALSE
to embed the code but not run it.
# Same exact code, but this time not run.
head(mtcars) # look at the first six rows
As you can see, R did not return the first six rows of
mtcars this time.
HOMEWORK: all of the code you produce for your homework will be embedded using these chunks, so I can see what you did. You will always provide code (in chunks) for any answers/plots that were obtained using code.
NB: When you type up code in a normal R script, it
will consist primarily of what’s in the chunks of an R Markdown
document. If you want to include plain English text for whatever reason,
you have to set it off with #, so R knows not to run that
text (like what I did in the above chunks).
You can also embed plots in an R Markdown file. The code for doing so is placed in chunks as before. You can also run the R plotting code within your R Markdown file using CTRL+Enter or Cmd+Return (as before).
# create histogram of Sepal.length in iris dataset
hist(iris$Sepal.Length)
HOMEWORK: always provide code (in chunks) for any plots you make!
I think that’s it! Again, if you ever have any questions, Google is your best friend when it comes to coding in R. The cheat sheet will also help a lot, and you can always ask me questions.
Fun fact: I code all of your in-class R tutorials using R Markdown!