Introduction to R Markdown
Learning Objectives
Knitting R Markdown to different formats
Understanding fundamental Markdown syntax
Using code chunks in R Markdown
Using code chunk options
Including plots & tables in R Markdown
Include a first-level header, second-level header, and third-level header in your R Markdown document naming them Section 1, Subsection 1, and Subsubsection 1, respectively.
Section 1
Here is something in R Markdown. R programming is cool.
Subsection 1.1
This is a subsection. It is basically a section of a section.
Subsubsection 1.1.1
This is a subsubsection. Now, this is a section of a section of a section.
Including a Hyperlink
Include a hyperlink in the R Markdown document linking to the Google homepage.
Google is a great resource when coding in R
What would we do without it!
Simple Text Formatting
Include an italicized word or phrase in your R Markdown document.
I like R Markdown because it is cool!
Include a bolded word or phrase in your R Markdown document.
I like R Markdown because it is cool!
Include a word styled showing it is code in your R Markdown document.
The following is a variable x <- 10
Include a superscript and a subscript in your R Markdown document.
Solve the following: y3 + 4log24
Code Chunks
# packageName::functionName(arg1 = 3, arg2 = 'linear')
knitr::include_graphics(path = "https://github.com/dilernia/STA418-518/blob/main/uptownFunk.png?raw=true")
Modify uptownChunk
by setting the echo chunk option to
be FALSE.
Creating a Scatterplot
Include the code to create a scatter plot above in a new chunk at the bottom of the R Markdown document.
library(tidyverse)
# Load storms data set
data(storms)
storms |> ggplot(aes(x = pressure, y = wind)) +
geom_point()
Inline R Code
The following R Markdown syntax: ?/ The 95% confidence interval for the mean is (-0.548, 4.548) produces:
The 95% confidence interval for the mean is (-0.548, 4.548).
Tables
# Seven largest sustained wind speeds recorded
storms_summary <- storms |>
group_by(name, year) |>
summarize(max_wind_knots = max(wind)) |>
ungroup() |>
slice_max(max_wind_knots, n = 4)
We can display the table without any helper functions like below.
## # A tibble: 4 × 3
## name year max_wind_knots
## <chr> <dbl> <int>
## 1 Allen 1980 165
## 2 Dorian 2019 160
## 3 Gilbert 1988 160
## 4 Wilma 2005 160
kable() function.
name | year | max_wind_knots |
---|---|---|
Allen | 1980 | 165 |
Dorian | 2019 | 160 |
Gilbert | 1988 | 160 |
Wilma | 2005 | 160 |
flextable() function.
library(flextable)
storms_summary |>
flextable() |>
set_caption(caption = "Table 1. Storm summary statistics with flextable()") |>
colformat_double(big.mark = "", digits = 0) |>
autofit()
name | year | max_wind_knots |
---|---|---|
Allen | 1980 | 165 |
Dorian | 2019 | 160 |
Gilbert | 1988 | 160 |
Wilma | 2005 | 160 |
Table themes
Display the storms_summary table applying a complete theme using the gt() function and the gtExtras R package by viewing the available themes here: https://jthomasmock.github.io/gtExtras/reference/index.html
library(gt)
library(gtExtras)
storms_summary |>
gt() |>
gt_theme_dark() |>
tab_header(title = "Table 1. Storm summary statistics with gt()")
Table 1. Storm summary statistics with gt() | ||
name | year | max_wind_knots |
---|---|---|
Allen | 1980 | 165 |
Dorian | 2019 | 160 |
Gilbert | 1988 | 160 |
Wilma | 2005 | 160 |
Display the storms_summary table applying a complete theme using the flextable() function by viewing the available themes here: https://davidgohel.github.io/flextable/reference/index.html#flextable-themes.
library(flextable)
storms_summary |>
flextable() |>
theme_zebra() |>
set_caption(caption = "Table 1. Storm summary statistics with flextable()") |>
colformat_double(big.mark = "", digits = 0) |>
autofit()
name | year | max_wind_knots |
---|---|---|
Allen | 1980 | 165 |
Dorian | 2019 | 160 |
Gilbert | 1988 | 160 |
Wilma | 2005 | 160 |