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

Figure 1: Guinea Pig tooth growth

Section headers

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.

first level headers

second level headers

third level headers

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.

  • one file
  • second file
    • third file
    • fourth file
    • fifth file
      • one last indentation
  1. First time
  2. Second item
  3. Third item
  1. subitem1
  2. subitem2
  3. subitem3

block quotes

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

formulas

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.

Code chunks

code chunk options

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

Table of contents

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

Tabs

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.

Themes

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

code folding

you can also use code_folding option to toggle between displaying the code and hiding the code. This is done with:

code_folding: hide

summary

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.