16  R Markdown

R Markdown integrates data analysis with documentation, letting you produce dynamic, reproducible reports and presentations. It combines the core syntax of Markdown — a simple markup language for formatting text — with embedded R code chunks, and can render a single source file to HTML, PDF, or Word.

16.1 Key Features

  • Reproducible research — code and report live in the same file, so the analysis behind a figure or table can always be re-run and checked.
  • Multiple output formats — one source file, several possible outputs (HTML, PDF, Word).
  • Dynamic content — results update automatically whenever the underlying R code or data changes, so a monthly yield report can be re-rendered against fresh data with no manual copy-pasting.
  • Tight RStudio integration — write, preview, and compile from one window.

16.2 Creating an R Markdown File

In RStudio: File → New File → R Markdown…, choose an output format (HTML, PDF, or Word), and give the document a title. RStudio opens a new file with a default template already in place.

16.3 The Default Template

YAML metadata — a block at the top of the file, enclosed in triple dashes (---), that sets the title, author, date, and output format:

---
title: "Monthly Yield Report"
author: "Vijay"
date: "2026-09-09"
output: html_document
---

Setup chunk — immediately follows the YAML header:

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

include=FALSE runs the chunk but hides it from the final document — the right place for repository options and global chunk settings, such as:

options(repos = c(CRAN = "https://cran.rstudio.com/"))
knitr::opts_chunk$set(message = FALSE)

After editing the template, save the file with a descriptive name and click Knit to compile it — this runs every code chunk and renders the result to the chosen output format.

16.4 Markdown Syntax

  • Headers# for a top-level header, ## for a subheader, and so on.
  • Bold and italic**bold** and *italic*.
  • Lists- or * for unordered lists, numbers for ordered lists.
  • Links[link text](URL).
  • Images![caption](image path).
  • Code chunks — three backticks followed by {r} open a chunk; three backticks close it. Code inside runs, and its output appears directly below.

16.5 Worked Example: A Field Report

A short R Markdown-style analysis of a season’s field data — summary statistics followed by a plot.

16.6 Useful Chunk Options

Option Runs code Shows code Shows output
eval = FALSE No Yes No
include = FALSE Yes No No
echo = FALSE Yes No Yes
message = FALSE / warning = FALSE Yes Yes Yes (messages/warnings suppressed)
error = TRUE Yes Yes Yes (render continues even if the code errors)
  • eval = FALSE — display example code without running it, or disable a block without commenting out every line.
  • include = FALSE — run the code, but hide both code and output; the standard choice for setup chunks.
  • echo = FALSE — hide the code, but keep the results; useful for a report meant for readers who don’t need to see the R behind it.

16.7 PDF Output: Installing tinytex

PDF output requires a LaTeX distribution. tinytex is the lightest option for R Markdown users:

Code
tinytex::install_tinytex(force = TRUE)

For more depth than this chapter covers, the R Markdown Cookbook is a thorough, freely available reference.

Summary

Concept Description
R Markdown
What R Markdown Is Combines Markdown text with embedded R code chunks to produce reproducible HTML, PDF, or Word reports.
Key Features Reproducible research, multiple output formats, dynamic content, and tight RStudio integration.
Creating a File and the Default Template File -> New File -> R Markdown; the default template has a YAML header, a setup chunk, and a Knit button to compile.
Markdown Syntax Headers, bold/italic, lists, links, images, and three-backtick code chunks.
Chunk Options eval/include/echo/message/warning/error control what runs and what's shown in the rendered document.
PDF Output PDF output needs a LaTeX distribution; `tinytex` is the lightest option to install.