this is a tutorial on how to do R markdown for reproducible researches. here we can type long passages without using “hashes” (for annotation).
in our first example, we are using toothGrowth dataset. in this experiment guinea pigs wee given different amount of vitamin C to seee the effect on the animal’s tooth growth.
to run a code in a markdown file, we need to denote the section that is considered R code.we call these sections “code chunks”.
below is the 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, by 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(lm(len ~ dose, data = toothdata))
figure: 1 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 wwith the “#” that we previously used to denote text in R script.
we can also add bullet point-type marks in our r markdown file.
we can put really nice quotes into the markdown document. we do this by using “>” symbol. > “Genes are like the story and DNA is the language that 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 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 markdown’s homepage for this example. [RMarkdown] (http://rmarkdown.rstudio.com/)
we can also put nice formatted 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 aslo options for your R Markdown file on how knitr interprets the code chunk. there are following options
Eval(T or F) eval prints the code chunk (input) whether or not evaluate the code chunk
echo (T or F) echo prints the output
cache: if enable, the same chunk code 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 separately.
out.width or out.height: the output size of R plot in the documnet.
fig.cap: the words for the figure caption.
## [1] "hello world"
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)
| title: “HTML tutorial” |
| output: |
| html_document: |
| toc: true |
| toc_float: true |
| date: “2024-06-06” |
this will us a very nice floating table of contents on the right hand side of the document.
you can also add tabs in the document for that you need to specify each section that you want to become a tab by placing this “{.tabset }” after the line. every subsequent header will be a new tab.
you can also add themes to your HTML Document that changes the highlighting color and hyperlink color of your html output. This can be nice aesthetically. To do this, you change your theme in YAML to one of the following.
cerulean journal flatly readable spacelab unite cosmo lumen paper sandstone simplex yeti null
you can aslo change colors by specifying highlights
default tango payments kate monochrome espresso zenburn haddock textmate
you can aslo use the code_folding option to allow the reader to taggle between displaying the code and hiding the code. this is done with:
code_folding: hide
There are tons of options and ways for yo customize your R code using the HTML format. This is also a great way to display a “Portiflio” of your R work if you are trying to market yourself to interested parties.