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

Here we can type long passages or descriptions of our data without the need of “hashing” out our comments with the # symbol. In our first example, we will be using the ToothGrowth daraset. In this experiemtn, 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 a markdown file, we need to denote the section that is considered 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 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 the “#” that we previously used to denote text in R script

First level header

Second level header

Third level header

Make sure that you put a space after the hashtag or it will not work

We can also add bullet point-type marks un 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 to note here that in R Markdown, indentation matters!

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

“Don’t tell the the sky is the limit when there are footprints on the moon.”

— Unknown

Formulas

We can also put nice formatted formulas into markdown using 2 dollar signs.

Hardy-Weinberg Formula

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

And you get really complex 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 ot 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 enabled, 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 DOC

fig.cap: words for the figure caption

Table of Contents

We can also add a table of contents to our HTML doc. We do this by altering the YAML code (the weird code chunk at the very top of the doc) We can add this:

title: “HTML_Tutorial” author: “Kambria Lemoine” date: “2024-06-21” output: html_document: toc: true toc_float:true

This will give us a very nice floating table of contents on the left 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 change the theme of the HTML document by changing 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 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 showing and hiding the code. This is done with:

code_folding: hide

Summary

There are many options for customizing your HTML document. This is also a great way to display a “portfolio” of your work.