This is a tutorial on working with R markdown for reproducible research.

Here we can type long passages or descriptions of our data without the need of the “hashtag” with our comments In the first example or experiment, literal Guinea Pigs were given different amounts of Vitamin C to analyze the effects on the animal’s tooth growth.

To run R code in markdown, 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 seen above, running the “play” results in a 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 Pigs when given variable amounts of Vitamin C

Figure 1: the tooth growth of Guinea Pigs when given variable amounts of Vitamin C

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 “#” that we previously used to denote text in an R script

First level header

Second level header

Third level header

Make sure there is a space after the hashtag, it will not work otherwise.

We can also add bullet points or bullet 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 is important here that in R markdown that 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 markdown document. We do this by using the “>” symbol.

“Those who fail to learn from their mistakes of their predecessors are destined to repeat them.”

— George Santayana

Formulas

We can also put nice formatted formulas into markdown using two dollars signs.

Hardy-Weinberg formula:

\[p^2 + 2pq + q^2 = 1\]

We can also get really complex… \[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]

Code Chunks

Code Chunk Options

There are also options for you R Markdown file on how Knitr interprets the code chunk. There are the following options:

Eval (T or F): Whether of 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, the same code chunk will not be evaluated the next time Knitr is runned. great for code 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 the file that are saved separately.

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 by altering the YAML code (The weird code chunk at the very top.) We can add this:

title: “HTML_Tutorial” author: “Thomas Rodsuwan” date: “2023-10-21” output: html_document: toc: true toc_float: true

This will give us a very nice floating “TOC” on the left side of the Knitr

Tabs

We 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 “{.tabse}” 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 cool. 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 the highlights:

default tango payments kate monochrome espresso zenburn haddock textam

Code Folding

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

code_folding: hide

Summary

There are a ton of options for us to customize our R code using the HTML format. This is also a great way to display a “portfolio of our work to try and market ourselves.