THis is a tutorial on how to use R markdown for reproducable research Here we can type long passages or descriptions of our data without the need of “hashtaging” out our comments with the # symbol. in our first example, we will be using the ToothGrowth dataset. In this experiment Guinea pigs were given different Vitamin C ammounts to see the effects on the animal’s teeth growth.
To run R code in markdown file, we need to denote the section that is considered R code. we call these sections “code chunks”
below is a code chunk:
Tootdata<- ToothGrowth
head(ToothGrowth)
## 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, the results are printed inline from the r markdown file.
fit<-lm(len ~ dose, data = ToothGrowth)
b<- fit$coefficients
plot(len ~ dose, data = ToothGrowth)
abline(lm(len ~ dose, data = ToothGrowth))
The slope of the regression line is 9.7635714
we can also put sections and subsections in our r markdown, similar to numbers or bullter points in a word document. this is done with the “#” that we previously used to denote text text in an R script
make sure there is a space after the hashtag before text or it will not work.
we can also add bullter point type marks in our r markdown file
its important to note 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 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 broser and will redirct the reader to the materials 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 nicely formatted formulas into markdown using two $ signs. Hardy-Weinberg Formula
\[p^2 + 2pq + q^2 = 1\]
you can get really complex as well
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
there are options for your R markdown file on how Knitr interprets the code chunk. there are ther following options.
Eval (T or F) : whether or not to evaluate code chunk
print("Hello World")
echo (T or F) : whether or not to show code for chunk but results will still print.
## [1] "Hello, World"
cache : if enebled, the same code chunk will not be evaluated the next time 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 sie of the R plots in the R document
fig.cap : the words for the figure caption
fit<-lm(len ~ dose, data = ToothGrowth)
b<- fit$coefficients
plot(len ~ dose, data = ToothGrowth)
abline(lm(len ~ dose, data = ToothGrowth))
Figure 1: the tooth growth of guinea pigs when given cariable amounts of Vitamin C
we can also add a table of contents to our HTML document. we do this by altering the YAML code (weird code chunck at beginning of code). We can add this:
title: “HTML_Tutorial” author: “Cassie_Wingate” date: “2024-06-28” output: html_document: toc: true toc_float: true
this will give us a nice 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 tab by placing “{.tabset}” after the line. Every subsequent header will be a new tab.
you can also add themes to HTML document that change the heighlighting color and hyperlink color. this can be nice aesthetically. to do this, change your 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 specifying highlights:
defult tango payments kate monochrome espresso zenburn haddock textmate
you can also use the 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 to customize your R doce using the HTML. THis is also a great way to display a porfolio of your work if you are trying to market yourself to interested parties.