This is a tutorial on how to use R markdown for repoducible research.
Here we can type a long passage or a description of our data iwhtout the need of “hashing” out our comments with the symbol “#”. In our first example, we will be using the ToothGrow data set. In this experiment we will see how Guinea Pigs were given different amounts of vitamin C to see the different effects of the animals tooth growth.
To run this in a mardown file, we need to denote the section that is considered R code, we call this section “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
As you can see, from running the “play” button on the chunk code, the results are printed inline of the r markdown file.
fit <- lm(len ~ dose, data = ToothData)
b <- fit$coefficents
plot(len ~ dose, data = ToothData)
abline(lm(len ~ dose, data = ToothData))
Figure 1: The tooth growth of Guinea Pigs when given variable amounts of vitaminC
the slope of the regression line is ‘r b[2]’.
We can also put sections and subsections in our r markdown file, similar to numbers or bullet points in a word doc. This is done with hashing.
Make sure you put a space after the hashtag, ptherwise it will not work.
We can also add bullet point-type marks in our r markdown file.
It is important to note that in r markdown indentation matters
We can put nice quotes into the markdown document. we do this by using the “>”
“Genes are like the story, and DNA is the language that the story is written in.”
– Sam Kean
Hyperlinks can also be incorperated into these files. This is especially useful in HTML files since they are in a we 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 formulas into the markdown usig 2 dollar signs.
Hardy-Weinberg Formula \[p^2 + 2pq +q^2 = 1\]
and you can make it complex as well
\[\Theta = \begin{pmatrix}\alpha &\beta\\ \gamma &\delta \end{pmatrix}\]
There are also options for R Markown file on hoe to knitr interprints the code chunk. there are the following examples
Eval (T or F): whether or not to evaluate the code chunk.
Echo (T or F): whether or not to show the code for the chunk, bu the results will still print.
Cache: If enable, the same chunk will not ne evaluated the next time that the knitr is ran.
fig.width or fig.height: the (graphical device) size of the r plots in inches. the figures are first written to the knit r document then to files that are saved sepeartely.
out.width or out.height: the output size of the R plot in the r document
fig.cap the words o=for the 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 TUTURIAL” author: “Carson Henry” date: “2024-11-11” 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 addd tabs to our report. You need to specify each section that you want to become a tab by placing {.tabset} after the line. Each subsequent header will be a new tab.
You can also add themes to you HTML document with highlighting color and hyperlink color of your HTML output. Do the following:
cerulean journal flatly readable spacelab united cosmo lumen paper sandstone simplex yeti null
You can also change the color by specifying highlight:
default tango payments monochromes espresso zenburn haddock textmate
you can also use the code_folding option to allow the reader to toggel between displaying the code and hiding the color by doing this:
code_folding: hide
There are a ton of options and ways you can customize your code usinf HTML format. This is grreat to display a “portfolio” of your rwork to show off to interest parties.