this is a tutorial on how to use R markdown for reproducible research.
Here we can type long passages or descriptions of our data without the need of “hashtaging” our comments with the # symbol. In our first example we will be using the ToothGrowth dataset. In this experiemnt Guinea pigs were given different amounts of vitaimin C to see the affects on the animals teeth growth
To run R code in a markdown file, we need to denote 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
As you can see, from running the “play” button results in the code chunk 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))
Guinea Pig Tooth Growth
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 word document. This is done with the “#” that we previously used to denote text in an R script.
make sure you put a space after the hashtag, otherwise it will not work!
we can also add bullet points-type marks in our r markdown file.
its important to note here that r markdown indentation matters!
we can put really nice quootes into the markdown document. We do this by using the “>” symbool.
“Genes are like the story, and DNA is the language that the story is written in.”
— Sam Kean
“If you ain’t first you’re last”
— Ricky Bobby
Hyperlinks can also be incorperated into these files. This is especially useful in HTML files, since they are in a web browser anf will redirect the reader to the material that you are interested in showing the. Here we will use the link to R markdown’s homepage for this example. RMarkdown
We can also put nice formated formulas into Markdown using two dollar signs.
Hard-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}\]
There are alsso options for your R markdown file on how Knitr interprits the code chunk. There are the following options.
Eval (T or F): whether or not to evaluate the code chunk
Echo (T or F): wheter or not to show the code for the chunk, but results will still print.
cache: if you enable, the same code chunk will not be evaluated the next time that 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 separately.
out.width or out.height: The output size of the R plots IN THE DOCUMENT.
fig.cap: the words for the figure caption