This is a tutorial on how to use R markdown for reproducable research.
Here we can type long passages or descriptions of our data with the need of “hashing” out our comments with a # symbol. In our first example, we will be using the ToothGrowth dataset. In this experiment, Guinea pigs were given different amounts of vitamin C to see the effects on the animal’s tooth groth.
To run R code in a markdown file, we need to denote the section that is considered R code. we call these “code chuncks”
Below is a code chunk:
## 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 chunck, 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))The slope of the regression is 9.7635714
Can also put sections and suvsections in r markdown files.This is done with hashtags
Make sure that you put a space after the hashtag it will not work
We can also add bullet point- type marks in our r markdown file.
Its important to know that in R markdown indentation is important
We can put quotes into the markdown document. We do this by using the “>” symbol.
“My name Jeff”
— Meme
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 them. Here we will use the link to R markdowns homepage for this example. RMarkdown
WE can also put formated formulas into Markdown using two dollar signs.
Hard-weinberg Formula
\[p^2 + 2pq + q^2 = 1\]
There are also options for your R markdown file on how kniter interprits the code chunk. There are the following options.
Eval (T or F): whether or not to evaluate the code chunk
ex
Echo (T or F): Whether or not to show the code for the chunk, but the results will still print
ex
## [1] "hello World"
fig.caption ex
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 Guiea pigs when given vitamins
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: “Alejandro Cervantes” date: “2025-04-09” output: html_document: toc: true toc_float: true
This will give us a floating table of contents on the right side of the document.
We can also add TABS in our report. To do this you need to specify that you want to become a tab by placing “{.tabset}” after this line. Every subsequent header will be a new tab.
You can also add themes to your HTML document that change the highlighting color and hyperlink color of your HTML output, To do this, you can change your theme in the YAML to one of the following:
spacelab sandstone yeti null (BUNCHMORE google)
You can also change the highlight color by following same process as above.
you can also use the code_folding option to allow the reader to toggle between displaing the code and hiding the code.This is done with:
code_folding: hide
There are a ton of options to customize R code using HTML format. Great way to display a “portofolio” of your work if your trying to market yourself to interested parties/ send link for recruiters.