this is a tutorial on how to do R markdown for reproducible researches. here we can type long passages without using “hashes” (for annotation).

in our first example, we are using toothGrowth dataset. in this experiment guinea pigs wee given different amount of vitamin C to seee the effect on the animal’s tooth growth.

to run a code in a markdown file, we need to denote the section that is considered R code.we call these sections “code chunks”.

below is the 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, by running the play button on the code chunk the 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))
 figure: 1 Guinea pig Tooth Growth

figure: 1 Guinea pig Tooth Growth

the slope of the regression line is 9.7635714.

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 wwith the “#” that we previously used to denote text in R script.

first level header

second level header

third level header

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
  1. first item
  2. second item
  3. third item
  1. subitem
  2. subitem

Block quotes

we can put really nice quotes into the markdown document. we do this by using “>” 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 dollar signs.

Hard-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} \]

code chunk

code chunk options

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

Eval(T or F) eval prints the code chunk (input) whether or not evaluate the code chunk

echo (T or F) echo prints the output

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

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 separately.

out.width or out.height: the output size of R plot in the documnet.

fig.cap: the words for the figure caption.

## [1] "hello world"

table 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)

title: “HTML tutorial”
output:
html_document:
toc: true
toc_float: true
date: “2024-06-06”

this will us a very nice floating table of contents on the right hand side of the document.

Tabs

you can also add tabs in the document for that you need to specify each section that you want to become a tab by placing this “{.tabset }” after the line. every subsequent header will be a new tab.

Themes

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

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

you can aslo change colors by specifying highlights

default tango payments kate monochrome espresso zenburn haddock textmate

code folding

you can aslo use the code_folding option to allow the reader to taggle between displaying the code and hiding the code. this is done with:
code_folding: hide

summary

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