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
Fitting basic models with R
In this activity, we will learn some fundamental concepts related to R and R Markdown. As we progress through the activity, the ➡️ symbols will prompt us to complete a task to facilitate a more interactive learning session.
➡️ First, create a new R Markdown document with File > New
File > R Markdown…, and then click . Knit the file by clicking the
Knit button (top left).
➡️ Create one new R Markdown document for each of the three most
common formats: HTML, PDF and Word. Knit each of the three
documents, noting the differences in style but consistency in the
information contained.
Note: If knitting to PDF causes an error, you may
need to install \(\LaTeX\), a markup
language popular in mathematics. To install \(\LaTeX\), first install the
tinytex
package by submitting
install.packages("tinytex")
to the Console (bottom left of
RStudio) and pressing return / enter, and then submitting
tinytex::install_tinytex()
into the Console as well.
For the rest of the activity HTML will be the recommended output format, but you are welcome to use whichever output format you prefer.
R Markdown is a tool for data analysis reports that integrates Markdown, a lightweight markup language for formatting plain text files, with R code and output. Markdown is designed to be easy to read and write, so it is not very customizable, but is designed with an emphasis on simplicity.
Headings and subheadings can be used to organize one’s document using Markdown syntax.
# 1st Level Header
## 2nd Level Header
### 3rd Level Header
➡️ Include a first-level header and second-level header in your R Markdown document naming them Section 1 and Subsection 1.1, respectively.
Markdown can be used to include hyperlinks and external images / GIFs in R Markdown documents as well.
<http://example.com>
[linked phrase](http://example.com)

➡️ Include a hyperlink in the R Markdown document linking to the Google homepage or your favorite website.
➡️ Include the following GIF at this URL (or another GIF you like from https://www.giphy.com)
Basic formatting of text, such as italicizing, bolding, superscripts, subscripts, and striking of text can be implemented as well.
Raw text input | Output |
---|---|
*italic* |
italic |
**bold** |
bold |
`code` |
code |
superscript^2^ |
superscript2 |
subscript~2~ |
subscript2 |
~~strikethrough~~ |
➡️ Include an italicized word or phrase in your R Markdown document.
➡️ Include a bolded word or phrase in your R Markdown document.
The real power of R Markdown comes from the ability to integrate simple formatting of text with code included in code chunks, which allow us to run R code inside our document:
```{r}
# Defining radius
radius <- 1
# Calculating area
area <- pi * radius^2
# Displaying result
area
```
There are three main ways to insert a code chunk:
The keyboard shortcut Cmd/Ctrl + Alt + I (recommended)
The “Insert” button icon in the editor toolbar
(located at the top right of RStudio).
By manually typing the chunk delimiters
```{r} and ```
.
Backticks, ```
, are used to enclose code chunks in R
Markdown documents, separating code from the rest of the document. Note
that we will only use R as the desired language for our code chunks, but
other programming languages are possible as well.
➡️ Include a code chunk containing the R code below. Then compile the
code chunk by pressing the button.
# Important message
stats_message <- "All models are wrong, but some are useful."
# Printing message
paste0(stats_message)
R packages are collections of R code and functions that others have
made available. R packages most commonly reside on the Comprehensive R Archive Network
(CRAN). To use an R package from CRAN, it must be installed using the
install.packages()
function and then loaded using the
library()
function.
For example, we can install the janitor
R package by
submitting install.packages("janitor")
to the Console pane
in the bottom of RStudio. We can then include a code chunk with the code
library(janitor)
to load the package when we knit our R
Markdown file.
➡️ Submit the following R code to the Console to install packages for the remainder of the activity.
# Creating vector of package names
packages <- c("tidyverse", "skimr", "flextable",
"ggfortify", "broom", "janitor")
# Installing package if not already installed
for(p in packages) {
if(length(find.package(p, quiet = TRUE)) == 0) {
install.packages(p)
}
}
Typically, it is best practice to load any R packages at the top of an R Markdown file or script for clarity and organization.
➡️ Insert a new code chunk to load R packages for this activity using the code below.
# Loading R packages
library(tidyverse)
library(skimr)
library(flextable)
library(ggfortify)
library(broom)
library(janitor)
Note that we have already installed these R packages for use in this
activity, but in general you may need to submit
install.packages('package_name')
to the Console first to
install a package called package_name
(replacing
package_name
with the name of the desired R package) if
this code yields an error saying
For the remainder of this activity, we will analyze the Palmer Penguins data set: a data set consisting of measurements collected on 344 penguins from 3 islands in Palmer Archipelago, Antarctica. Specifically, data were collected and made available by Dr. Kristen Gorman and the Palmer Station, Antarctica Long Term Ecological Research Network1.