Intro

Welcome to R Markdown! R Markdown allows us to share our results and/or code in an easily understandable format for decision makers, other researchers, and as a “lab notebook” for yourself to keep track of the steps you perform in a project.

Here are some resources we’ll be going through:

   

An R Markdown document is composed of three components.

  1. a YAML header that describes info like the title, author, and formatting directions. More here.

  2. R code chunks

  3. text which can be formatted with bolding, italics and color

   

YAML header

The YAML header allows us to customize the colors and style of our code syntax, formatting, create a table of contents, and choose the output format we will knit.

Here’s what mine looks like for this html document:

See Authoring Books with R Markdown for more options and themes. There are so many themes!

   

Code chunks

We create code chunks in R Markdown with this format

# ```{r name_of_chunk, ..., echo = F}
# code
# ```

We can also specify how we want R to run that code block.

  • echo = F
  • warning = F
  • message = F
  • error = F

   

Echo = T

##    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

   

Echo = F

##    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

   

Error = T

## Error in head(TeethGrowth): object 'TeethGrowth' not found

   

Text

We write text in R Markdown by typing directly into our source window.

   

There are a few options to create links:

   

To bold a word we type **word** or __word__.

   

To italicize a word we type *word* or _word_.

   

To color a word we type <span style="color:red">word</span>.

   

To make headers we can use the # symbol.

## Header 2
### Header 3

Header 2

Header 3

   

To make bullet points or numbers we can use the * or 1. symbol.

* bullet 1
1. number 1
  • bullet 1
  1. number 1

   

To insert a picture, we type ![](../picture name)

   

Tables

You can make tables by hand in R Markdown. This code ->

turns into this once we knit our document:

Food Milligrams (mg) per serving Percent (%) DV*
Red pepper, sweet, raw, ½ cup 95 158
Orange juice, ¾ cup 93 155
Orange, 1 medium 70 117
Grapefruit juice, ¾ cup 70 117
Kiwifruit, 1 medium 64 107
Green pepper, sweet, raw, ½ cup 60 100
Broccoli, cooked, ½ cup 51 85
Strawberries, fresh, sliced, ½ cup 49 82

Source

   

You can also make a table by running some dplyr code.

## # A tibble: 6 x 4
## # Groups:   supp [2]
##   supp   dose     n  mean
##   <fct> <dbl> <int> <dbl>
## 1 OJ      0.5    10 13.2 
## 2 OJ      1      10 22.7 
## 3 OJ      2      10 26.1 
## 4 VC      0.5    10  7.98
## 5 VC      1      10 16.8 
## 6 VC      2      10 26.1

   

You can make this into a prettier stylized table using kable() in the knitr package and functions from the kableExtra package.

supp dose n mean
OJ 0.5 10 13.23
OJ 1.0 10 22.70
OJ 2.0 10 26.06
VC 0.5 10 7.98
VC 1.0 10 16.77
VC 2.0 10 26.14

More kable styling at: https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html

   

We can also make an interactive table using datatable() in the DT package. This will let us navigate and search our dataset.

More DT styling at: https://rstudio.github.io/DT/

   

We can make a nice-looking Table One using kableone() from the tableone package.

Overall
n 60
len (mean (SD)) 18.81 (7.65)
supp = VC (%) 30 (50.0)
dose (mean (SD)) 1.17 (0.63)

More tableone styling at: https://cran.r-project.org/web/packages/tableone/tableone.pdf

   

We can make fancy well-formatted tables for regression model data results using stargazer().

Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
len 60 18.813 7.649 4.200 13.075 25.275 33.900
dose 60 1.167 0.629 0.500 0.500 2.000 2.000
Results
Dependent variable:
len
(1) (2)
dose 9.764*** 7.811***
(0.953) (1.195)
suppVC -8.255***
(2.236)
dose:suppVC 3.904**
(1.691)
Constant 7.423*** 11.550***
(1.260) (1.581)
Observations 60 60
R2 0.644 0.730
Adjusted R2 0.638 0.715
Residual Std. Error 4.601 (df = 58) 4.083 (df = 56)
F Statistic 105.065*** (df = 1; 58) 50.355*** (df = 3; 56)
Note: p<0.1; p<0.05; p<0.01
Correlation Matrix
len dose
len 1 0.803
dose 0.803 1

More stargazer styling at: https://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf

   

Graphs

You can also embed plots into your R Markdown, for example:

   

Remember that we can animate our graphs using plotly!

   

Extras

Bookdown like R for Data Science, wow! https://bookdown.org/

   

Pretty Documents https://prettydoc.statr.me/themes.html

   

Blogdown for websites https://awesome-blogdown.com/

   

   

Let’s try!