This is a tutorial on how to use R markdown for reproduceable research.
Here we can type long passages or descriptions of our data without the need to hash out comments with the #. In our first example we will be using the ToothGrowth dataset. It is about supplying vitamin C to guinea pigs and seeing how much their teeth grow.
To run R code in a markdwon file, we need to denote the section that is the R code. We call these sections “code chunks”.
Below is a code chunk ` in top left, under esc:
## 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 form 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: Guinea Pig Tooth Growth
The slope of the regression line 9.7635714.
Save file and give it a name in files. Click save again to bring it up as a HTML.
We can also put sections and subsection in our RMarkdown file. Similar to numbers or bullet points in a word document. This is down with a hashtage that we previously used as an annotation in a R script.
Have to have a space after your hashtag.
We can also add bullet point-type marks in our r markdown file.
It’s important to note here 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 browser and will redirect the reader to the material you want to show them. Here we we use the link to R Markdown’s homepage as an example.
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} \]
LaTex cheat sheet
## Code chunk options
There are also options four your R Markdown file on how knitr interprets code chunk. There are the following options.
Eval (T or F). Evaluates whether or not to run the code chunk or not. Shows the R code but does not print the result.
Echo (T or F). Does not show the R code but does print the result.
## [1] "Hello World"
This following code chunk will print nothing
Cache: If you enable cache the same chunk will not be evaluated the next time that the knitr is run. Great for code that has long run times. CAREFUL. If change a value above a code chunk that is cached, it will not change in the code below.
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. Only changes size of the saved files that are external to a knit document.
out.width or out.height: output size of the R plots in the R DOCUMENT. e.g., HTML, Pdf files
fig.cap: create words for the figure caption
We can also add a table of contents to our HTML Document. We do this by altering the YAML code (Weird code chunk at the very top at the document)
title: “HTML_tutorial” author: “Alice Bourne” date: “2024-10-22” output: html_document: toc: true toc_float: true
This will give a floating table of contents 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 code by placing {.tabset} after the line. Every subsequent header will be a new tab. Needs to be on a level 1 header.
You can also add themes to and HTML document that change the highlighting colour and theme colour of your HTML output. This can be nice asthetically. To do this you change you theme in YAML to one of the following:
cerulean journal flatly readable spacelab united cosmo lumen paper sandstone yeti null
You can also change the colour by specifying highlight:
default tango payments kate monochrome espresso zenburn haddock textmate
You can also use the code folding option to allo the reader to toggle between displaying the code and hiding the code. This is done with:
code_folding: hide
Ton of options to customise R code using HTML format
Also a great way to display a “portfolio” of you rwork if you are trying to market to interested parties.