R Markdown

YAML Header

---
title: "My Report"
author: "Your Name"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_float: true
    theme: cosmo
    code_folding: show
---
# Other output formats
output: pdf_document
output: word_document
output:
  html_document:
    css: custom.css
  pdf_document:
    latex_engine: xelatex

Text Formatting

# Heading 1
## Heading 2
### Heading 3

**bold text**
*italic text*
***bold and italic***
~~strikethrough~~
`inline code`
[link text](https://url.com)
![alt text](image.png)

> blockquote

- bullet item
  - sub-item
1. numbered item
2. second item

---   (horizontal rule)

Code Chunks

```{r chunk-name, echo=TRUE}
summary(mtcars)
```

Insert chunk: Ctrl+Alt+I (Win) / Cmd+Option+I (Mac)

# Key chunk options:
echo    = TRUE   # show code?
eval    = TRUE   # run code?
include = TRUE   # show output?
warning = FALSE  # show warnings?
message = FALSE  # show messages?
error   = TRUE   # continue on error?
cache   = TRUE   # cache results?

# Figure options
fig.width  = 7   # inches
fig.height = 5
fig.cap    = "Caption text"
fig.align  = "center"    # left/right
out.width  = "80%"       # in document
dpi        = 300

Global Chunk Options

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo    = TRUE,
  warning = FALSE,
  message = FALSE,
  fig.width  = 7,
  fig.height = 4,
  fig.align  = "center"
)
library(tidyverse)
```

Place at top of document. The setup chunk runs first.

Inline Code

There are `r nrow(mtcars)` cars in the
dataset. The mean MPG is
`r round(mean(mtcars$mpg), 1)`.

# Renders as:
# There are 32 cars in the dataset.
# The mean MPG is 20.1.

Tables

# knitr::kable — simple and reliable
```{r}
mtcars |>
  head(5) |>
  knitr::kable(
    caption = "First 5 cars",
    digits  = 1,
    col.names = c("MPG","Cyl","Disp",
      "HP","Drat","Wt","QSec","VS",
      "AM","Gear","Carb")
  )
```

# kableExtra — enhanced formatting
library(kableExtra)
kable(df) |>
  kable_styling(
    bootstrap_options = c("striped","hover"),
    full_width = FALSE
  ) |>
  row_spec(1, bold = TRUE, color = "red")

# gt package — publication-quality
library(gt)
df |>
  gt() |>
  tab_header(title = "Summary") |>
  fmt_number(columns = value, decimals = 2)

Figures

```{r scatter-plot, fig.cap="MPG vs Weight",
    fig.width=6, fig.height=4,
    fig.align="center", out.width="70%"}
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  theme_minimal()
```

# Multiple plots side by side
```{r two-plots, fig.show="hold",
    out.width="50%"}
hist(mtcars$mpg)
hist(mtcars$wt)
```

# Include external image
![Caption](path/to/image.png){width=50%}

# Or with knitr
knitr::include_graphics("plot.png")

Cross-references & Citations

# Cross-reference (bookdown)
output: bookdown::html_document2

See Figure \@ref(fig:scatter-plot).
See Table \@ref(tab:summary-table).
See Section \@ref(methods).

# Bibliography — add to YAML:
bibliography: references.bib
csl: apa.csl          # citation style

# Cite in text
@smith2024 found that...
Results were significant [@smith2024].
[@smith2024; @jones2023]
[-@smith2024]          # suppress author

# references.bib entry:
@article{smith2024,
  author  = {Smith, J.},
  title   = {Title Here},
  journal = {Journal Name},
  year    = {2024}
}

Output Formats

# HTML with options
output:
  html_document:
    toc: true
    toc_depth: 3
    number_sections: true
    theme: flatly        # or: cosmo, lumen,
                         # sandstone, journal
    highlight: tango

# PDF (requires LaTeX)
output:
  pdf_document:
    toc: true
    number_sections: true

# Word
output: word_document

# Slides
output:
  ioslides_presentation  # HTML slides
  slidy_presentation     # HTML slides
  beamer_presentation    # PDF slides
  xaringan::moon_reader  # advanced HTML

# Render from console
rmarkdown::render("report.Rmd")
rmarkdown::render("report.Rmd",
  output_format = "pdf_document")

Parameterized Reports

# YAML
params:
  year: 2024
  region: "Northeast"

# Use in code
```{r}
df |> filter(year == params$year,
             region == params$region)
```

# Render with different params
rmarkdown::render("report.Rmd",
  params = list(year = 2025,
                region = "West")
)