This is a tutorial on how to use R Markdown for Reproducible research

Here we can type long messages or long descripitions of our data without hashtags before the text. In our first example we will be using ToothGrowth dataset. In this experiment, the rodent, Guinea Pigs (literal) were given varying amounts of vitamin c. This was done to see the effects on the animal’s tooth growth.

To run code in a Markdown File, we need to denote the section that is considered actual 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 we can see, from running the “play” functionality in the code chunk, the results have been printed in the 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 files, similar to numbers or bullet points in a word document. This is done with the “#” that we previously used to denote text in an R Script.

First Level Header

Second Level Header

Third Level Header

ENSURE you have have a space after the “#”. If not present, it will fail to understand what you are trying to accomplish.

We can also add bullet point-type marks in our R Markdown file

  • one item
  • one item
  • one item
    • one more item
    • one more item
    • one more item
      • one more 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 markdown document. We do this by using the “>” symbol.

“Now cracks a noble heart. Good night, sweet prince, and flights of angels sing thee to thy rest.”

— William Shakespeare

Formulas

We can also insert really nice formulas into R Markdown using two dollar sigs

Hard-Weinberg Formula

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

And you can get really complex ones as well

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

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 chunks

Echo (T or F): Whatever or not to show the code for the chunks, but results 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 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.

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. We do this altering the YAML code (the weird code chunk at the VERY top of the documents.) We can add this:

title: “HTML Tutorial 1,2,3” author: “Karsten Condron” date: “2024-06-30” output: html_document: toc: true toc_float: true

This will give us a very nice floating table of contents on the right hand 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 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 nice aesthetically. To do this, you can change your theme in the YAML to one of the following

cerulean journal flatly 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 reader to toggle between displaying the code and hiding the code. This is done with:

code_folding: hide

Summary

There are a ton of different aspects and avenues to mess around with in R Markdown “HTML” when compared to the traditional R Script. Making a sort of portfolio within this is very easily done.