How to use rmarkdown for reproducible research. We can type long passages or descriptions of our data without the need of hashtag. For example: we will be using ToothGrowth dataset. In this experiment, Guinea Pigs 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 denotate 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
fit <- lm(len ~ dose, data = Toothdata)
b <- fit$coefficients
plot(len ~ dose, data = Toothdata)
abline(lm(len ~ dose, data = Toothdata))
Figure1: The tooth growth of guinea pigs
The slope of the regression line is 9.7635714
We can also put sections and subsections in our r markdown file, similar to numbers or bullet points in a wordd document. This is done with the “#” that we perviously used to
Make sure you put a space after the hashtag, otherwise it will not work!
we can also add bullet point-type marks in our r markdwon file.
Its imporatant to note here that in R Markdown identation matters!
We can really put nice quotes into the markdown document. We do this by using the “>” symbol.
“Genes are like story, and DNA is the language that the story is written in.”
— Sam Lean
Hyperlinks can also be incorporated into these files. This is especially useful in HTML files, since they are in web browser and will redirect the redaer 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.
Hardy-Weinberg Formula
\[p^2 + 2pq + q^2 = 1\] And you get really complex as well!
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
print ("Hello World")
## [1] "Hello World"
There are also options for your R Markdown file on how knitr iinterprits code chunk. There are the following options.
Eval (T or F): whether or not to evaluate. Echo (T or F): whether or not to show the code for the chunk, but results will. cache: If enable, the same code chunk will not be evaluated the next time that the knitr is run, Great for code that has long runtime. 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. gig.cap: can write 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: “Sukhpreet Kaur” date: “2024-04-05” output: html_document: toc: true toc_float: true
This will give us a very nice floating table of contents.
You can also add TABS in our report. To do this you need to sepcify each section that you want to become a tab by placing the code “{.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 hyperlink color of you html output. This can be nicely aesthetically. To do this, you change your theme in the YAML to one of the following:
cerulean journal flatly readable spacelab united cosmo lumen paper sandstone simplex yeti null darkly bootstrap
You can also change the color by specifying highlight:
default tango payments kate monochrome espresso zenburn haddock textmate
You can also use code folding option to allow the reader to toggle between displaying the 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.