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 or comments with the “#” symbol. In our first example, we will be using the ToothGrowth dataset. In this expt., Guinea Pigs were given different amounts of vitamin C to see the effects on animal’s tooth growth.

To run R code in a markdown file, we need to denote the section that is considered R code. We call these sections “Code chunks”

Below is a code chunk:

Toothdata <- ToothGrowth

head(Toothdata)
##    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
tail(Toothdata)
##     len supp dose
## 55 24.8   OJ    2
## 56 30.9   OJ    2
## 57 26.4   OJ    2
## 58 27.3   OJ    2
## 59 29.4   OJ    2
## 60 23.0   OJ    2

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, main = "Tooth Length vs Vitamin C Dose", xlab = "Vit. C Dose", ylab = "Tooth Length")
abline(lm(len ~ dose, data=Toothdata))
Figure 1: Guinea Pig tooth growth when given various levels of Vit. C

Figure 1: Guinea Pig tooth growth when given various levels of Vit. C

summary(lm(len ~ dose, data=Toothdata))
## 
## Call:
## lm(formula = len ~ dose, data = Toothdata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.4496 -2.7406 -0.7452  2.8344 10.1139 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   7.4225     1.2601    5.89 2.06e-07 ***
## dose          9.7636     0.9525   10.25 1.23e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.601 on 58 degrees of freedom
## Multiple R-squared:  0.6443, Adjusted R-squared:  0.6382 
## F-statistic: 105.1 on 1 and 58 DF,  p-value: 1.233e-14

The slope of the regression line is 9.7635714

Section Headers

We can also put sections and subsections in our r 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 an R Script.

First Level Header

Second Level Header

Third Level Header

Make sure you put a space after the hashtag otherwise it will not work!

We can also add bullet point-type marks in our r markdown file.

  • one item
  • one item
  • one item
    • one more item
    • one more item
    • one more item
      • one last item

It’s important to note here that in R markdown, indentation matters!

  1. First item
  2. Second item
  3. Third item
  1. Sub-item 1
  2. Sub-item 2
  3. Sub-item 3

Block Quotes

We can put really nice quotes in the markdown document. We do this by using the “>” symbol.

“Genes are like the story, and DNA is the language that the story is written in.”

— Sam Kean

Formulas

We also put nice formatted formulas into Markdown using two dollar signs.

Hardy Weinberg Formula

\[p^2 + 2pq + q^2 = 1\] And you can get really complex as well!

\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]

A good reference for different codes within R that you can use is in the following link: LaTex Cheat Sheet

Code Chunks

Code chunk options

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.

Echo (T or F): whether or not to show the code for the chunk, but results will still print.

Cache: If you enable cache, the same code chunk it will not be evaluated the next time knitr is run. Great for code that has LONG runtimes.

fig.width or fig.height: the (graphical device) size of the R plot in inches. The figures are first written to the knitr document then to files that are saved regularly.

out.width or out.height: The output size of the R plots IN THE R DOCUMENT.

fig.cap: the words for the figure caption.

Table of Contents

We can also add a table of contents to our HTML document. We do this by altering the YAML code (the weird code chunk at the VERY top of the document.) We can add this: title: “HTML_Tutorial” author: “Tyler Paquin” date: “2025-03-14” output: html_document: toc: true toc_float: true

This will give us a very nice floating table of contents on the left hand side of the document.

Tabs

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.

Themes

You can also add themes to your HTML document that change the highlighting color and hyperlink color of your html output. This can be nice asthetically. To do this, you can change your theme in the 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 textmate

Code Folding

You can also use the code_folding option to allow the reader to toggle between displaying and hiding the code. This is done with: code_folding: hide

Summary

There are a TON of options and way for you to customize your R code using the HTML format. This is also a great way to display a “portfolio” of your work if you are trying to market yourself to interested parties.