This is a tutorial on how to use RMarkdown for reproducible research Here we can type long passages describing our code without the need of #

We will take ToothGrowth dataset

In this experiment, Guinea Pigs were given different amounts of vitamin C to see effects on the animal’s tooth growth.

To run R code in markdown we have 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

So, by running the play button, results from codes in the code chunk gets printed in the markdown

fit <- lm(len ~ dose, data = toothdata)

fit
## 
## Call:
## lm(formula = len ~ dose, data = toothdata)
## 
## Coefficients:
## (Intercept)         dose  
##       7.422        9.764
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 amount of Vitamin C

Figure 1: The tooth growth of Guinea pigs when given variable amount of Vitamin C

The slop of the regression line is 9.7635714

Section Headers

We can put sections and subsections in our rMarkdown files, similar to number or bullet points in a word document. This is done with ‘#’. In R script we use # for marking text in code.

First level header

Second level header

Third level header

Make sure to put space after # otherwise it will not work

We can also add bullet point-type marks in rMarkdown file.

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

its important to know that, in rMarkdown indentation matter!

  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 can also put nice formatted formulas into markdown using 2 dollar sign.

Hard-Weinburg formula

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

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

code chunks

code chunk options

print("hello world")
## [1] "hello world"

There are also options for your rMarkdown file on how to knitr interpret 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 be printed.

Cache: If enable, the same code chunk will not be evaluated the next time the knitr is run. Great for codes that has long run times.

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. Good for publication purposes.

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 add ToC to our HTML document. We do this by altering the YAML code (the weird code chunk of the very top of the document). We can add this

title: “HTML Learn” author: “Dipan” date: “2026-05-10” output: html_document: toc: true toc_float: true

This will give us a very nice floating ToC.

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 anew tab.

Themes

You can also add themes to your HTML document that change the highlighting and hyperlink color. 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 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 ways for you to customize your R code using the HTML formatting. Its a great way to display portfolio of work, if you want to market yourself.