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 to “hashtag” out our comments with the # 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 file, we need to denote the section that is considered R code. We call these “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

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))

The slope of the regression line is 9.7635714.

Section Headers

We can also put sections and subsections in our markdown file, similar to numbers or bullet points in a word doc. This is done with the #. *must have a space after #

First Level Header

Second Level Header

Third Level Header

We can also add bullet point-type marks in our markdown file. *hit tab to indent, indentation matters in Markdown!

-one item -one item -one item -one more item -one more item -one more item -one last item

  1. first item
  2. second item
  3. third item

Block Quotes

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

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

—Sam Keen

Formulas

We can also put nice formated formulas into r Markdown using two dollar signs

Hard-Weinberg formula \[p^2+2pq=q^2=1\]

You can get really complex as well

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

LaTex cheatsheet

print("Hello World")

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 or not

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

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 runtimes.

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.

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

fig.cap: words for figure captions

Table of contents

We can also add a table of contents to our HTML document. We do this by altering the YAML code (weird code chunk at the VERY top of the document)

we can add this:

title: “HTML_tutorial” author: “Erin Timm” date: “2025-09-25” output: html_document toc: true toc_float: true

this will give is a very nice floating table of contents on the right hand side of the document.

Tabs

We can also add tabs on 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.

You can also add themes to your HTML document that change the highlighting color and hyperlink color. This can be nice aesthetically. To do this you 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 readewr to toggle between displaying the code and hiding the code.

code_folding: hide

Summary

There are a TON of options and ways 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.