1 R Markdown

R Markdown is a format for writing reproducible, dynamic reports with R. Use it to embed R code and results into slideshows, PDFs, HTML documents, Word files and more.

2 Structure

  1. YAML Header
  2. Text Area
  3. Code chunk

3 Workflow

  1. Open – Open a file that uses the .Rmd extension.
  2. Write – Write content with the easy-to-use R Markdown syntax.
  3. Embed – Embed R code that creates output to include in the report.
  4. Render – Replace R code with its output and transform the report into a slideshow, PDF, HTML or MS Word file.

4 Text Area Formatting Tools:

  • Plain text
  • End a line with two spaces to start a new paragraph italics and italics bold and bold superscript2 strikethrough Hyperlink: [CCIDM]

5 Header 1

5.1 Header 2

5.1.1 Header 3

5.1.1.1 Header 4

5.1.1.1.1 Header 5
5.1.1.1.1.1 Header 6

endash: – emdash: — inline equation: \(A = \pi r^2\) slide break:


Block quote

  1. Ordered list 1
  2. Ordered list 2
    • sub-item 1
    • sub-item 2
Table Header Second Header
Table Cell Cell 2
Cell 3 Cell 4

6 YAML Header

YAML header is a set of key: value pairs at the start of your file. Begin and end the header with a line of three dashes (—). (From the Rmd cheatsheet

https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf)

You will notice that my YAML header is different from the default one Rmd file automatically created. That is because I customized output option as above. This setting dictates how your HTML file that will be knitted will look like.

  • date: ‘2026-01-25 20:41:40’
    • This allows to print the current time each time you knit the file.
    • Can be customized in various ways
      • date-time: ‘2026-01-25 20:41:40.765384’
      • Day, Month, date, Year (e.g., Sunday, January 25, 2026): ‘Sun, Jan, 25, 2026’
      • m/d/Y (e.g., 01/25/2026): ‘01/25/2026’

toc: yes
- I turned the table of contents on. This will have hyperlinks in HTML and allow readers to navigate to the desired sections faster.

  • toc_depth: 4
    • I want to see up to four levels of headings.
  • toc_float: true
    • This allows to move from the front end of the document to a left side floating when you scroll down.
    • By default, the floating toc headings are collapsed, not showing all headings. We can turn it off using the following.
    toc_float:
    collapsed: false
    • highlight: espresso
    • I want to see espresso style: black background of codes area.
  • theme: journal
    • Choose the style you like.
  • number_sections: yes
    • Turned on automatic section numbering, which is why I didn’t add numbering manually below.
    • Notice how this will nicely turn your HTML report much more organized.
    • Try this and other options and see which one you like the most in your HTML file.

7 Embed Codes

7.1 Code chunk

7.1.1 Sample

  • Ctrl + Alt + I
# use Ctrl + Enter simultaneous to execute a line
# Use # to comment out a code

# install.packages("tidyverse")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# install.packages("palmerpenguins")
library(palmerpenguins)
## 
## Attaching package: 'palmerpenguins'
## 
## The following objects are masked from 'package:datasets':
## 
##     penguins, penguins_raw
# install.packages("skimr")
library(skimr)

penguins
## # A tibble: 344 × 8
##    species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
##    <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
##  1 Adelie  Torgersen           39.1          18.7               181        3750
##  2 Adelie  Torgersen           39.5          17.4               186        3800
##  3 Adelie  Torgersen           40.3          18                 195        3250
##  4 Adelie  Torgersen           NA            NA                  NA          NA
##  5 Adelie  Torgersen           36.7          19.3               193        3450
##  6 Adelie  Torgersen           39.3          20.6               190        3650
##  7 Adelie  Torgersen           38.9          17.8               181        3625
##  8 Adelie  Torgersen           39.2          19.6               195        4675
##  9 Adelie  Torgersen           34.1          18.1               193        3475
## 10 Adelie  Torgersen           42            20.2               190        4250
## # ℹ 334 more rows
## # ℹ 2 more variables: sex <fct>, year <int>
dim(penguins)
## [1] 344   8
skimr::skim(penguins)
Data summary
Name penguins
Number of rows 344
Number of columns 8
_______________________
Column type frequency:
factor 3
numeric 5
________________________
Group variables None

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
species 0 1.00 FALSE 3 Ade: 152, Gen: 124, Chi: 68
island 0 1.00 FALSE 3 Bis: 168, Dre: 124, Tor: 52
sex 11 0.97 FALSE 2 mal: 168, fem: 165

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
bill_length_mm 2 0.99 43.92 5.46 32.1 39.23 44.45 48.5 59.6 ▃▇▇▆▁
bill_depth_mm 2 0.99 17.15 1.97 13.1 15.60 17.30 18.7 21.5 ▅▅▇▇▂
flipper_length_mm 2 0.99 200.92 14.06 172.0 190.00 197.00 213.0 231.0 ▂▇▃▅▂
body_mass_g 2 0.99 4201.75 801.95 2700.0 3550.00 4050.00 4750.0 6300.0 ▃▇▆▃▂
year 0 1.00 2008.03 0.82 2007.0 2007.00 2008.00 2009.0 2009.0 ▇▁▇▁▇
# sex distribution
penguins%>%
  count(sex) %>%
  ggplot(aes(sex,n)) +
  geom_col()

# Number of male
sum(penguins$sex == "male", na.rm = TRUE)
## [1] 168
# Total observation
nrow(penguins)
## [1] 344
# Percent of Males
m_per = round((sum(penguins$sex == "male", na.rm = TRUE) / nrow(penguins)) * 100, digits = 1)

# Percent of Females
f_per = round((sum(penguins$sex == "female", na.rm = TRUE) / nrow(penguins)) * 100, digits = 1)

7.1.2 Code chunk options

For more details visit https://yihui.org/knitr/options

7.2 Inline coding

  • Sample characteristics:
    There are 344 penguins in the data, 48.8 % of which are males and 48 % of which are females.

9 Expectations for Organizing your assignment report with Rmd file

9.1 M01-1 Principle Assignment