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 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 a markdown file, 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 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 = ToothGrowth)
b <- fit$coefficients
plot(len ~ dose, data = ToothGrowth)
abline(lm(len ~ dose, data = ToothGrowth))
The slope of the regression is 9.7635714.
We can also put sections and subsections in an R Markdwon file, similar to numbers or bullet points in a word document. This is done with the “#” that we used previously to denote text in a script.
Make sure you put a space after the hashtag, otherwise it will not work!
We can also add bullet point-type marks.
Its important to note here, in R Markdown, INDENTATION MATTERS!
We can similarly do ordered lists.
We can also put nice quotes into the markdown package. We do this by using the “>” symbol.
“Genes are like the store, and DNA is the language that the story is written in.”
— Sam Kean
Hyperlinks can also be incorporated into these files. This is especially 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. Here we for this example we will link to the R Markdown homepage for your reference. RMarkdown
We can also put nice formatted formulas into Markdown using two dollar signs.
Hardy-Weinberg Formula
\[p^2 + 2pq + q^2 = 1\]
And you can get really complex as well!
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
If you are more interested in how to do these formulas in R Markdown, you can check out this LaTex cheatsheet.
There are also options for your R Markdown file on how knitr interperits the chunk. There are 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, results will still print.
Cache: If enabled, the same code chunk will not be evaluated the next time the document is run. Great for long running processes, but can pose a problem if you change any values above the code.
fig.width or fig.height: The (graphical device) size of R plots in inches. The figures are first written to the knitr document then to files that are saved seperatly.
out.width or out.height: The output size of the R plots in the output document.
fig.cap: the words for the figure caption.
We can also add a table of contents to our HTML document. We do this by altering our YAML code (the weird code chunk at the VERY top of the document.) We can add this:
title: “HTML Markdown Handbook” author: “Dr. VDB” date: “3/1/2022” output: html_document: toc: true toc_float: true
This will give us a very nice floating table of contents on the right side of our HTML file.
If you wan to change it to having the TOC always show and not appear as you scroll, you can change the setting “collapsed” to FALSE.
Alternatively, you can have numbered sections by using the number_sections: TRUE
We can also create TABS in our report. To do this you need to specify each seaction you want to become a tab by placing {.tabset} after the line. Every subsequent subheader will be a new tab.
You can also add themes to your HTML document that changes the highlighting color and hyperlink color of your makrdown file. This can be be nice ascthetically. To do this, you need to change your theme:
cerulean journal flatly readable spacelab united cosmo lumen paper sandstone simplex yeti null
You can also change the highlight color by specifying highligh:
default tango pyments kate monochrome espresso zenburn haddock textmate
You can also use the code_folding option to allow the reader to toggle between displaying code and hiding the code. this is done with:
code_folding: hide
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 “portfolio” of your work if you are trying to market yourself to interested parties.