This is tutorial on how to use R markdown for reproducible research
Here we can ytpe long passages without using hash. We’ll use Toothgrowth dataset In this experiment guinea pigs were given vitamin C to see effects on animal tooth growth
To run R code in a markdown file, we need to dentote the section that is considered R code. We call this section as code chunks
below is code chunk:
Toodata <- ToothGrowth
head(Toodata)
## 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 playy button on the code chunk, the results are printed inline of the r markdown file
fit <- lm(len ~ dose, data = Toodata )
b <- fit$coefficients
plot(len ~ dose, data = Toodata)
abline(lm(len ~ dose, data = Toodata))
Figure 1: Guinea Pig tooth growth
we acn also put section and subsection in rmarkdown file,similar to numbers or bullet points in word documents. This is done with # that we previously used to denote text in an R script.
make sure to put space after hash tag otherwise it doesn’t work. we can also make the bullet point in our rmd file. rmd file is strict in identation and to make this bullet point we have to put the space before starting it.
we can put really nice quotes into rmarkdown document. We do this by using < symbol
“genes are like the story and DNA is language that story is written in”
— Sam kean
HYPERLINKS can also be corporated to 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 link to R markdown’s homepage for this example. RMarkdown
we can also put nice formatted formula into markdown using two dollar signs
Hardy 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}\]
latex cheat sheets for the formula making, you can have look if you want.
There are also options for your R markdown file on how knitr interprets the code chunk. These are the following options.
Eval(T or F): whether or not to R to evaluate the code chunks, and does not show output but code is visible when it gets compiled with knitter or not
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 when the knitr is run. Great for code that has long runtimes.
fig.width or fig.height : the graphical device size of the R plots in inches. The figures are first written to knitr document then to files that are saved separately.
out.width or out.height : the output size of the R plots in the R document
fig.cap: the words for figure caption
we can also add table of contents to our HTML document. We do this by altering the YAML code(the weird code chunk at very top of the document)
title: “html_tutorial” author: “Swati” date: “2023-09-23” output: html_document: toc: true toc_float: true
This will give really nice floating table of contents in 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 your html document that change the highlighting colour and hyperlink color of your html output. This can be aesthetically nice. To do this change your theme in the YAML to one of the following
cerulean spacelab journal flatly readable united cosmo lumen paper sand stone simplex yeti sandstone null united
You can also change the colour by specifying the highlights:
default tango monochrome kate payments espresso zenburn haddock textmate
you can also use code_folding option to toggle between displaying the code and hiding the code. This is done with:
code_folding: hide
there are ton of options and way for you to customize your R code using html format. This is also great way to display a portfolio of your r work if you are trying to market yourself interested parties.