R Markdown is a powerful tool for creating dynamic documents that integrate code, text, and visualizations as well as for creating reproducible reports. By combining text, code, and output in a single document, you can effectively communicate your data analysis results.

Step 1: Install R Markdown

Ensure you have R and RStudio installed. Then, install the rmarkdown package as well as any other package that your code will need to use:

install.packages("rmarkdown")

Step 2: Create a New R Markdown Document

  1. Open RStudio.
  2. Go to File > New File > R Markdown....
  3. You can just choose the “Document” option and you can specify it as pdf (this can easily be changed in the yaml header too so it does not really matter)
  4. Fill in the title, author, and select the output format.
  5. Click OK to create the document.

Step 3: Understand the R Markdown Structure

An R Markdown document consists of three main parts:

---
title: "My First R Markdown"
author: "Your Name"
date: "October 3, 2024"
output: html_document
---
## Introduction

This is a simple R Markdown document.
summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Download this file titled ``tutorial-markdown.Rmd” and open it in your R Studio. You will see that the code that generates the above embedded R code and output. If you simply open this.Rmd file and then render it by clicking on “Knit”, you will have the output file.

Step 4: Add Content to Your Document

Step 5: Render the Document

Step 6: Customize Output

output:
  html_document:
    toc: true
    toc_depth: 2