this is a tutorial on how to use rmarkdown for reproducible research.
Here we can type long passages or decription of our data without the need of “hashing” out our comments with teh # symbol. In our first example, we willl be using the ToothGrowth dataset. In experiment, Guinea Pigs (literal) were given different amounts of vitmin C to see the effects on the animal’s tooth growth.
To run R code in a rmarkdown file, we need to dentote 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 on the code chunk, 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(fit)
Figure 1: Guinea pig toothgrowth
the slope of the regression line is 9.7635714
We can also put section 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 that you put a space after the hashtag, otherwise it will not work!
we can also add bullet point-type marks in our r markdown file by putting a dash
Its important to note here that in r markdown indentation matters!
We can put really quotes into the markdown document, we do this by using the “>”symbol
“Genes are like the story, and DNA is the languages that the story is written in.”
— Sam Kean
Hyperlinks can also 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 makrdown’s homepage for this example RMarkdown
We can also put nice formatted formulas into Markdown using two dollar signs.
Hard-Weinberg formula
\[p^2 + 2pq + q^2 = 1\]
And you get really complex as well!
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
LaTex ## Code chunk options
print("Hello world")
There are also options for your rmarkdown file on how kintr 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): whether or not to show the code for the chunk, but results will still print.
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 plot in inches. The figures are first written to the knitr document then to files that are saved separately.
out.width or ou.height: the output size of the R plots in the R document
fig.cap: the words for the figure caption
we can also add a table of contents to out HTML document. we do this by altering the YAML code (the weired code chunk at the very top of the document). we can add this;
title: “HTML-Tutorial” author: “Jiamao Yang” date: “2024-07-07” output: html_document: toc: true toc_float: true This will give us a very nice floating table of contents on the right hand side of the documents.
You can also add Tabs in our report. To do this you need to specify each section that you want to bencome a tab by placing “{.tabset}”after the line. Every subsequent header will be a new tab.
You can also add themes to your HTML ducument that change the highlighting color and hyperlink color of your html output. This can be nice asthetically. To do this, you change your theme in the YAML to one the following:
Cerulean journal flatly readable united cosmo lumen
You can also change the color by specifying highlights
default tango payments
you can also use the code folding option to allow the reader to toggle between displaying the code and hiding the code. this is
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 “portoflio” of you rwork if you are trying to market yourself to interested parties.