This is a tutorial on how to use R markdown for reproduciable research.
Here we can type long passages or descriptions of our data without the need of “hashing” out our comments with the hash tag symbol. In our first example we will be using the ToothGrowth data set. In this experiment, guinea pigs were given different amounts of vitamin C to see the effect on the animals tooth growth.
To run R code in a Markdown file, we need to denote the section that is considered R code. We call these sections “code chunk”.
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 line 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.
The slope of the regression line is 9.7635714
We can also put sections and subsections in r markdown similar to numbers or bullet points in a word document. This is done with the ‘#’ that we previously used to dennote text in R script.
Make sure that you put a space after the hashtag otherwise it will not work
We can also add bullet point type marks in our markdown file.
Its important to note here that in R markdown indentation matters, unlike regular R scripts.
We can put really nice quotes into the markdown documents, we do this by using the “>” symbol.
“Genes are the like the story and the DNA is the language of the story.”
— Sam Keen
Hyperlinks can also be incoorperated to these files. This is useful in HTML files since they are in a web and will redirect readers into material you are interetsed in showing. Here we will use the link to Rmarkdown homepage for the example. Rmarkdown
We can also put nice formatted formulas into rmarkdown using 2 dollar signs.
Hard-Weinber Formula
\[p^2+2pq+q^2=1\]
And you can get really complex,
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
There are also options for your R markdown file for knitr which compiles files into word/htm/pdf file. Knitr interprets the code chunks There are the following options:
Eval (T or F): It determines whether or not to determine the code chunk
other thing we can do is:
Echo (T or F): which is the opposite of Eval, it shows the results, but we can choose whether or not to print the code chunk
Cache: if you enable cache, the same code chunk will not be evaluated the next time knitr is run. It is great for code the 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 documents then to the file that are saved separately.
out.width or out.height: is the output size of the R plots in the R documents (in HTML file or PDF file)
fig.cap: the words for the figure discription
We can also add a table of contents to our HTML content. We can do this by altering our YAML code (the weird code chunk at the very top of the document). We can add this:
title: “HTML_tutorial” author: “Firas” date: “2024-09-29” output: html_document: toc: true toc_float: true
This will give us a nice floating table of content on the right hand side of the document.
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.
You can also add themes to your HTML document that change the highlighting color and the hyperlink color of the HTML output. This can be nice aesthetically. To do this you change the 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 specificity highlight:
defaults tango payments kate monochrome espresso zenburn haddock textmate
You can also use the code_folding option, under the YAML also, to allow the reading to toggle between reading the code and hiding the code. This is done by:
code_folding: hide