This is tutorial on how to use R markdown for reproducible research.

Here we can tpe long passages or discriptions of our data without the need of “hashing” out our comments with the # symbol. 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 markdown file , we need to denotethe section that is considered R code. we call these “code chunks”

Bellow 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” botton 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))
Figure1: Guinea pig Tooth Growth

Figure1: Guinea pig Tooth Growth

The slope of regression line is 9.7635714.

Section Header

We can put section 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 a R script.

First level header

Second level header

Third level header

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

We can also make bullet point-type r markdown file.

  • one item
  • one item
  • one item
    • one more item
    • one mote 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. subitem 1
  2. subitem 2
  3. subitem 3

Block Quotes

We can put really nice quotes into the 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

Code Chunks

Code chunk options

## [1] "Hello world!"

There are also options for your R Markdown file on how knitr interprits 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 result will still print.

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

fig.width or fig.height: the (graphical device) size of the R plots in inches. the figure are first written to knitr document then to files that are saved separately like powerpoint or PDF or etc.

out.width or out.height: the output size off the R plots in the R document

fig.cap: the words for figure caption

Table of Contests

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_Tuorials” author: “Maryam” date: “2026-06-20” output: html_document: toc: true toc_float: true

This will give us a very nice floating table of contents on the right hande 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 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 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 color by specifying highlight:

default tango payments kate monochrome espresso zenburn haddock textmate

Code Folding

You can also use code_folding in YAML option to allow the reader to toggle between displaying the code 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 format. This is also a great way to display a “Portofilio” of your works if you are tryng to market yourself to interested parties.