this is a tutorial on how to use R markdown for reproducible research Here we can type long passage or descriptions of our data without the need of “hashing” our our comments with the # symbol.

In our first example we will be using the ToothGrowth data set. In this experiment, Guinea pigs were given different amounts of vitamin C to see the effects of animal’s tooth growth.

To run R code in ta 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 on the code chunk, the results are printed online the r markdown file.

fit <- lm(len ~ dose, data = ToothData)

b <- fit$coefficients

plot(len ~dose, data = ToothData)

abline(lm(len ~ dose, data = ToothData))

The slope of the regression line is 9.7635714

dataset <- iris
plot(Sepal.Length ~ Sepal.Width, xlab = "Sepal Length", ylab = "Sepal Width", data = dataset)
abline(lm(dataset$Sepal.Length ~ dataset$Sepal.Width))

Gene <- c("HSPA4", "HSPA5", "HSPA8", "HSPA9", "HSPA1a", 'HSPA1b')
 AminoAcid <- c(840, 654, 493, 679, 641, 641)
 Nucleotide <- c(54537, 6491, 4648, 21646, 2400, 2517)
 
 HSPs <- data.frame(Gene, Nucleotide, AminoAcid)
plot(HSPs$AminoAcid ~ HSPs$Nucleotide, xlab = "Amino Acid", ylab = "Nucleotide", data = HSPs)
abline(lm(HSPs$AminoAcid ~ HSPs$Nucleotide, data = HSPs))

We can also put sections and subsection in our r markdown file similar to numbers or bullet points in a word document. This is dine with the “#” that we previously used to denote text in an R script.

1st level Header

Second level Header

Third level Header

Make sure that you put a space after the hash tag otherwise it will not work!

Code chunks

Bullet points

  • a
  • b
  • c
  • a
  • b
  • c
    • a Its important to note that here in R markdown indentation matters
  1. a
  2. b
  3. c a- a’ b- b’ c- c’

Block Quotes

We can put really nice quotes into the markdown document. We do this by using the “>” symbol.

“Genes are like the story, and DNA is the language the storry is written in.”

— Sam Kean

Fomrulas

We can also put nice formatted formulas to Markdown using two dollar signs.

Hard wiener formula

\[p^2+ 2pq+ q^2 =1\]

More complex formulas

\[ \Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]

For more complex formulas you can use Latex cheat sheet.

Code chunk options

There are also options for your R Markdown file on how knitr interprets the code chunk. There are the following options.

Eval(T or F): whether or not to show the results for the chunk, code will still show. Echo(T or F): whether or not to show the code for the chunk, results will still show.

Cache

If enabled, the same code chunk will not be evaluated the next time that Knitr is run. Great for code that has LONG run times. Be careful here: If you change something above what you cached the value will not change!!

Figure modifications

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. (This only saves the dimensions of the fig that are saved separately)

out.width or out.height: The output size of the R plots IN the R document (HTML file)

fig.cap: words for the figure caption

Table of contents

We can also add a table of contents by altering the YAML code (the code written automatically at the top of the document)

title: “HTML_Tutorial” author: “Koussai Bouker” date: “r Sys.Date()” output: html_document: toc: true toc_float: true

the last line toc_float is for the floating table of content ( to the side and floating)

Tabs

You can also add TABS in our report. To do this you need to specif 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 these to your HTML document that change the highlighting color and hyperlink color of your html output. This can be nice aesthetically. To do this, you change your theme in the YAML to one of the following:

You do this by adding theme: x to the YAML

x can be:

cerulean journal flatly readable spacelab united cosmo lumen paper sandstone simplex yeti null

Highlight

you can also change the color bu specifying highlight:

you do this by adding highlights: x to the YAML

x can be

default tango payments kate monochrome espresso zenburn haddock textmate

you can either add a theme nor highlight not both together!!

Code Folding

You can use this to allow for hiding the code and give the user the option to see it by clicking a link.

you do this by adding code_folding: hide to the YAML

Summary

Ton of options and ways for you to customize you R code using HTML formatting. This is also a quick and great way to display a portfolio of you work if you are trying to market yourself to interested parties.