This is a tutorial on how to use R markdown for reproducible research. Here, we can type long passages withot “hashing” out comments with the # symbol as in case of the traditional R script. In our first example, we will be using the toothGrowth dataset.In this experiment, Guinea pigs were given different amounts of vitamin C to see the effects on the animals 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
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 Pig when given variable amounts of vitamin C
The slope of the regression line is “r b[2]”
We can also put sections and subsections in R Markdown files, similar to numbers or bullet points in a word document. This is done with the “#” we previously used to denote text in an R script.
Make sure that you put space after the hash tag, otherwise it will not work!
We can also add bullet points in R Markdown file.
Note: In R Markdown, indentation is very important!
We can put really nice quotes into the R Markdown document by using the “>” symbol.
“Genes are like the story and DNA is the language that the story is written in.”
— Sam Kean
Hyperlinks can be also incorporated into these files. this is specially useful in HTML files, since they are in a web browser and will redirect the reader to the material that you are interested in showing them. Here, we will use the link to R Markdown’s home page for this example. RMarkdown
We can also put nicely formatted formulas in R Markdown using two dollar signs.
Hard Weinberg Formula
\[p^2 + 2pq + q^2 = 1\]
We can also write complex formula
\[\Theta = \begin {pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
There are also options for R Markdown on how knitr interprets the code chunk. There are the following options:
eval (T or F): whether or not evaluates the code chunk. To test this lets write a code chunk here:
echo (T or F): whether to show the code of the chunk, but the results will still print.
cache: If you enable the cache, the same code chunk will not be evaluated the next time that the knitr is run. This is great for code that has LONG run times. Be careful with caching things. It can be helpful when you do genomic stuff, but it can be bad or add errors to your code if you changed something that your code chunk depended upon.
fig.width or fig.height: will change the (graphical device) size of the R plots in inches. The figures are firstly 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: the word for figure caption
We can also add table of contents to our HTML document.We do this by altering the YAML code (the weired code chunk at the very top of the document). We can add this
title: “HTML_Tutorial” author: “Dr. Abdullah Hoter” date: “2026-05-29” output: html_document: toc: true toc_float: true
This will give us a very nice floating table of content on the right hand side of the document .
You can also add tabs on the 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 highlighting color and hyperlink 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 highlights:
default tango payments kate monochrome espresso zenburn haddock textmate
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 many options to customize your R code using the HTML format.This is a great way to display a “portfolio” of your work if you are trying to market your self to interested parties.