This is a tutorial on how to use R markdown for reproducible research.
Here we can type long passages or descriptions of our data without the need of “hashing” out our comments with # symbol. In our first example, we will be using the ToothGrowth dataset. In this experiment, Guinea Pigs (literal) were given different amounts of vitamin C to see the effects on the animal’s tooth growth.
To run R code in a markdown, we need to denote the section that is considered code. We call these sections “code chunks.”
Below is a code chunk:
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
As you can see from running the “play” button on the code chunk, the results are printed inline of the r markdown file.
fit <- lm(len ~ dose, data = Toothdata)
b <- fit$coefficients
plot(len ~ dose, data = Toothdata)
abline(lm(len ~ dose, data = Toothdata))Figure 1: The tooth growth of Guinea Pigs when given variable amounts of vitamin C.
The slope of the regression line is 9.7635714.
We can also put sections and subsections in our markdown file similar to numbers or bullet points in a word document. This is done with the “#” that we previously used to denote text in our R script.
Make sure that you put a space after the hashtag, otherwise it will not work.
We can also add bullet-like marks in our R Markdown file.
It’s important to note here that in R Markdown indentation matters.
We can put really nice quotes into Markdown. We do this by using the “>”.
“Genes are like the story, and DNA is the language that the story is written in.”
—Sam Kean
Hyperlinks can also be incorporated into these files. This is especially useful in HTML, since they are in a web broswer and will redirect the reader to the material that you are interested in showing to them. Here we will use the link to R Markdown’s homepage for this example.
We can also put nicely formatted formulas into Markdown using two dollar signs.
\[p^2 + 2pq + q^2 = 1\]
You can get really complex as well.
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
There are also options for your R Markdown file on how knitr interprets the code chunk. There are the following options:
Eval (T or F): Whether or not to evaluate the code chunk. Turns off sections of code.
Echo (T or F): Whether or not to show the code for the chunk but the results will still print. Opposite of Eval.
Cache: If enabled, the same code chunk will not be evaluated the next time that knitr is run. This is great for code that has long run times. If something above cached chunk is changed, the value will not get saved.
fig.width or fig.height: the (graphical device) size of the R plots in inches. The figures are first written to the knitr document, then to files that are saved separately (jpeg or png). So this option only affects the figure saved outside of R markdown file.
out.width or out.height: the output size of the R plots in the R file.
fig.cap: the words for the figure caption.
We can also add a table of contents to our HTML document. We do this by altering the YAML code (the top code chunk at the very top of the document).
title: “HTML Tutorial” author: “Melissa Barkemeyer” date: “2025-03-23” output: html_document: toc: true toc_float: true
This will give us a floating table of contents on the left side of the document.
You can also add TABS in our report. To do this, you need to specify each section that you want to become a tab by placing “{.tabset}” after the line. Every subsequent header will be a new tab. Must be on a level one header.
You can also add themes to your HTML doc that change the highlighting color and hyperlink color of your html output. To do this, you change your theme in YAML to one of the following:
cerulean journal flatly readable spacelab united cosmo lumen paper sandstone simplex yeti null
You can also change the color by specifying highlight:
default tango payments kate monochrome espresso zenburn haddock
You can also use the code_folding option to allow the reader to toggle between displaying the code and hiding the code. This is done with:
code_folding: hide
There are a ton of option and ways for you to customize your R code using the HTML format. This is also a great way to display aa “portfolio” of your work if you are trying to market yourself to interested parties.