Intermediate

R Markdown & Reporting

Combine R code, results, and narrative in reproducible documents using R Markdown and Quarto.

R Markdown Basics

R Markdown (.Rmd) files let you weave together code and text. When you "knit" the document, R executes the code, captures the output, and produces a formatted document.

R Markdown (.Rmd)
---
title: "Quarterly Sales Report"
author: "Data Team"
date: "`r Sys.Date()`"
output: html_document
---

## Overview

This report analyzes Q1 2024 sales data.

```{r setup, include=FALSE}
library(tidyverse)
library(knitr)
```

```{r load-data}
sales <- read_csv("sales.csv")
summary(sales)
```

## Visualization

```{r sales-plot, fig.width=10, fig.height=6}
ggplot(sales, aes(x = month, y = revenue)) +
  geom_col(fill = "steelblue") +
  theme_minimal()
```

Total revenue: **`r sum(sales$revenue)`** dollars.

YAML Header

The YAML header (between ---) controls the document metadata and output format:

YAML
---
title: "My Report"
author: "Your Name"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_float: true
    theme: flatly
    code_folding: hide
---

Code Chunk Options

OptionDefaultDescription
echoTRUEShow the code in output
evalTRUEExecute the code
warningTRUEShow warnings
messageTRUEShow messages
includeTRUEInclude chunk in output at all
fig.width7Figure width in inches
fig.height5Figure height in inches
cacheFALSECache results for faster re-rendering

Output Formats

  • html_document — Interactive HTML report (most common)
  • pdf_document — PDF via LaTeX (requires tinytex)
  • word_document — Microsoft Word .docx
  • ioslides_presentation — HTML slides
  • xaringan — Advanced HTML presentations
  • flexdashboard — Interactive dashboards

Parameterized Reports

R Markdown
---
title: "Sales Report"
params:
  region: "North"
  year: 2024
output: html_document
---

```{r}
data |> filter(region == params$region, year == params$year)
```
R
# Render with custom parameters
rmarkdown::render("report.Rmd", params = list(region = "South", year = 2025))

Quarto: Next-Gen R Markdown

Quarto is the successor to R Markdown, supporting R, Python, Julia, and Observable JS. It uses .qmd files with similar syntax:

Quarto (.qmd)
---
title: "My Analysis"
format: html
execute:
  echo: true
---

```{r}
library(tidyverse)
mtcars |> ggplot(aes(wt, mpg)) + geom_point()
```

Shiny Apps Basics

Shiny lets you build interactive web applications directly from R:

R
library(shiny)

ui <- fluidPage(
  sliderInput("bins", "Number of bins:", 10, 50, 30),
  plotOutput("hist")
)

server <- function(input, output) {
  output$hist <- renderPlot({
    hist(mtcars$mpg, breaks = input$bins, col = "steelblue")
  })
}

shinyApp(ui, server)