This is a tutorial on how to use R markdown reproducible research.

Here we can type long passages or descriptions of our data without the need of “hashing” out our comments with the #. In our first example, we will be using the ToothGrowth dataset. In this experiment, Guinea pigs were given different amounts of vitamin C to see the effects on the animal’s tooth growth.

To run our code in a markdown file, we need to denote the section that is considered R code. We call these sections as “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, th 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))

The slope of the regression line is ‘r b[2]’

Section Headers

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 with the “#” what we had used previously for texting on an R script. You can see what happens if you click the arrows next to the line number of the headers.

First Level Header

Second Level Header

Third Level Header

Make sure that you put a space after the hastag, otherwise it will not work !!!

We can also add bullet point-type marks in our R markdown file.

  • one item
  • one item
  • one item
    • one more item
    • one more item
    • one more item
      • one last item For the “sub-bullet points” of the bullet points, we pressed “TAB”.

It is important to note here that in R markdown indentation matters !!!

  1. First Item
  2. Second Item
  3. Third Item
    a sub item 1 b sub item 2 c sub item 3

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 languge that the story is written in.”

— Sam Kean

Formulas

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

Hard-Weingberg Formula1

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

And you get realy complex as well!

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

Maybe you would like to check LaTeX (cheat sheet)

Code Chunks

Code Chunk Options

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

Eval (T or F): whether or not to evaluate the code chunks.

print("Hello World")

Now, lets change the code chunk as “{r eval = F}” and knit. Do you see what happens? This time, the HTML file did not show the outcome “Hello World”, it just showed the code chunk.

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

## [1] "Hello World"

What happens if we also write “echo = f, eval = F” ?

It shows nothing.

Cache: If enable, the same code chunk will not be evaluated the next time that the kintr is run. This helps for the codes that has LONG run times.

fig.width or fig.height: the (graphical device) size of the R plots in inches. The figures are written first to the kintr then to the files that are saved separately.

out.width or out.height: The output size of R plots IN THE R DOCUMENT

fig.cap: the words for the figure caption. Let’s recall the chnuk that we have in the line 25, but we will change it a little.

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

b <- fit$coefficients

plot(len ~ dose, data = Toothdata)

abline(lm(len ~ dose, data = Toothdata))
Fgireu 1: The tooth growth of Guinea Pigs when given variable amoutns of Vitamin C

Fgireu 1: The tooth growth of Guinea Pigs when given variable amoutns of Vitamin C

Table of Contents

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. ) We can add this :

title: “HTML_tutorial” author: “Metehan Ustundag” 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 document.

Tabs

You can also add TABS in a 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. But it only works with First Level Headers.

Themes

You can also add themes to your HTML documents that change the highlighting color and hyperlink color of your HTML output. This can be nice aesthetically. To do this, you can change your theme in the YAML to one of the following:

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

You can also change the color by specifiyng highligth:

default tango payments kate monocrhome espresso zenburn haddock texmate

Code Folding

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

code_folding: hide

the following is the final version of the YAML:

title: “HTML_tutorial” author: “Metehan Ustundag” date: “2024-07-07” output: html_document: toc: true toc_float: true theme: united highlights: tango code_folding: hide

Summary

There are TON of options and ways for you to customize your R code using the HTML format. This is alsoo a great way to display a “portfolio” of your work if you are trying to market yourself to interested parties.