this is a tutorial on how to use r markdown for reproducible research

here we can type long passages or descriptions of our data without the need of “hashing” out our comments with the # symbol. in our first example. we will be using the toothgroth dataset.in this experiment. guniea pigs (literal) were given different amounts of vitamin c to see the effects on the animal’s tooth growth>

to run r code in a markdown file we need to dentote the section that is considered r code. we call these “code chunks”

below is a code chunck:

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 “play” button on the code chunk , the result 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: the tooth growth of guinea pigs when given variabl amount of vitamin c

figure 1: the tooth growth of guinea pigs when given variabl amount of vitamin c

The slope of the regression line is 9.7635714.

Section Headers

we can also put section and subsections in our r markdown file . similar to numbers or bullet points in a word document. This is done with “#” that we previously used to denote text in r script.

First level header

seconde level header

third level header

make sure that you put a space after the hash tag 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

    it is important to note that in r markdown indentation matters!

    1. First item
    2. second item
    3. third item
    1. subitem 1
    2. subitem 2
    3. subitem 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 language that the story is written in.”

—sam kean

Formulas

we can also put nice formatted formulas into Markdown using two dollars signs.

Hard-weinberg formula

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

And you get really complex as well!

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

Latex

code chunks

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 EVALUATE THE CODE CHUNK

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

Cache: if enable, the same code chunk will not be evaluated the next time that knitr is run. great for code that has long run times.

fig.width or fig.height: the (graphical device) size of the r plot in inches. the figures are first written to the knitr document then to files are saved separately.

out.width or out.height: the output size of the r plot in the r document.

fig.cap: the words for the figure caption.

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: “yazan” date: “2024-01-03” output: html_document: toc: True toc_float: true

this will give us a very nice floating table of the content on the 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 color and hyperlink color of your html output. this can be nice astheticaly. to do this, you change your theme in the YAML to one of the following:

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

you can also change the color by specifying highlight:

default tango payment kate monochrome espresso zenburn haddock textmate

code folding

you can alos use the code_folding option to allow the reader to toggle between displaying the code and hiding the code. this done with:

code_folding: hide

summary

there are a ton of options and ways for you to customize your r code using the HTML format. this is also a great way to display a “portoflio” of rwork if you are trying to market yourself to interested parties.