This is a tutorial on how to use R markdown for reproductible research.
Here we can type long passages or description of our data without the need of “hashing” out or comments with the # symbol. In our first example, we will be using the ToothGrowth dataset. 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 in lineof the r markdown file.
fit <- lm(len ~ dose, data = Toothdata)
b <- fit$coefficients
plot(len ~ dose, data = Toothdata)
abline(lm(len ~ dose, data = Toothdata))
The slope of the regression line is 9.7635714
We can also put sections and subsections in our r mardown file, 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.
Make sure that you put a space after the hashtag, otherwise it will not work!
We can also add bullet points-type marks in our r markdown file.
Its important to not here that in R markdown indentation matters!
We can put really nice quotes into the markdown 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
Hyperlinks can also be incorporated into these files. This is especially useful in HTML files, since they are in a web browswe and will redirect the reader to the material that you are interested in showing them. Here we will use the link to R Markdown’s homepage for this example. Rmarkdown
We can also put nice formatted formulas into Markdown using two dollar signs.
Hard-Weinburg Formula
\[p^2 +2pq +q^2 = 1\]
And you get really complex as well!
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
There are also options for your R Markdown file on how knitr interprints the code 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, 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 seperately.
out.width or out.height: The output size of the R plots in the R document.
fig.cap: the wrods for the figure caption
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_Tutorial” author: “Emma Olsen” 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 right hand side of the document.
You can also add TABS in our report. To do this 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.