100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

R Markdown Basics

Learn how to combine R code, output, and narrative text into reproducible documents, reports, and slides using R Markdown.

Practical RBeginner8 min readJul 10, 2026
Analogies

What R Markdown Is and Why It Matters

R Markdown lets you interleave prose written in Markdown with executable R code chunks in a single .Rmd file, so a document's narrative, the code that produced a result, and the result itself never drift out of sync the way a copy-pasted screenshot into a Word report would. Calling rmarkdown::render("report.Rmd") — or clicking Knit in RStudio — re-runs every code chunk from a clean session and weaves the output directly into the final HTML, PDF, or Word document, which makes the whole report reproducible from source rather than hand-assembled.

🏏

Cricket analogy: An .Rmd file weaving code and prose together is like a Cricinfo match report that auto-embeds the live scorecard widget — the commentary and the numbers can never fall out of sync the way a manually typed recap could.

Anatomy of an .Rmd File

Every .Rmd file opens with a YAML header delimited by --- lines specifying title, author, date, and output (e.g., html_document, pdf_document, or word_document), followed by ordinary Markdown prose for headings, bold text, and lists, interspersed with fenced R code chunks that begin with three backticks and {r}. The document reads top to bottom exactly like the final rendered report, which is what makes R Markdown approachable for people coming from writing plain Markdown or Word documents rather than a traditional IDE-only workflow.

🏏

Cricket analogy: The YAML header setting title, author, and output format is like a scorecard's header specifying the match, venue, and format (Test, ODI, T20) before a single ball is bowled — it frames everything that follows.

Code Chunks and Chunk Options

Each code chunk can carry options inside the curly braces, such as {r plot-sales, echo=FALSE, fig.width=8, cache=TRUE}echo=FALSE hides the code but still shows its output, include=FALSE runs the code silently without showing code or output (useful for loading packages), fig.width/fig.height control plot dimensions, and cache=TRUE skips re-running a slow chunk on subsequent knits as long as its code hasn't changed. Setting global defaults once with knitr::opts_chunk$set(echo = FALSE, warning = FALSE) at the top of the document avoids repeating the same options on every single chunk.

🏏

Cricket analogy: Setting echo=FALSE to hide code but show the output is like a broadcast showing only the ball-tracking graphic without the raw Hawk-Eye sensor data — the audience sees the result, not the underlying computation.

markdown
---
title: "Monthly Sales Report"
author: "Data Team"
date: "2026-07-10"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(dplyr)
library(ggplot2)
```

## Summary

Total revenue this month was `r scales::dollar(sum(sales$amount))`,
a change of `r round(mean(sales$growth) * 100, 1)`% from last month.

```{r plot-sales, fig.width=8, fig.height=4, cache=TRUE}
ggplot(sales, aes(x = month, y = amount)) +
  geom_col(fill = "steelblue") +
  labs(title = "Monthly Revenue", x = NULL, y = "Revenue ($)")
```

Global chunk defaults set with knitr::opts_chunk$set() apply to every subsequent chunk, but any option specified in an individual chunk's braces overrides the global default for that chunk only — so you can set echo=FALSE globally and still write {r echo=TRUE} on one chunk where you want to show the code.

Rendering to Different Output Formats

The output field in the YAML header determines the rendered format — html_document for a self-contained web page, pdf_document for a typeset PDF via LaTeX, word_document for a .docx file editable by non-technical stakeholders, or ioslides_presentation/revealjs::revealjs_presentation for slides — and switching between them often requires no changes to the body of the document at all. Parameterized reports take this further: declaring params: region: "West" in the YAML and referencing params$region in the code lets you call rmarkdown::render("report.Rmd", params = list(region = "East")) to regenerate the same report for a different region without touching the source file.

🏏

Cricket analogy: Switching output from html_document to pdf_document without changing the body is like the same match commentary being repackaged as a live text feed on one platform and a printed newspaper column on another — same content, different distribution format.

Rendering to pdf_document requires a working LaTeX installation — RStudio will prompt you to install tinytex via tinytex::install_tinytex() the first time, and missing LaTeX packages (e.g., for special fonts or symbols) can cause a knit that works fine in HTML to fail silently in PDF. If you don't need print-quality typesetting, html_document avoids this dependency entirely and renders faster.

  • An .Rmd file interleaves Markdown prose with executable R code chunks so text and results can never drift out of sync.
  • The YAML header (between --- lines) sets title, author, and output format before the document body begins.
  • Chunk options like echo, include, fig.width, and cache are set per-chunk or globally via knitr::opts_chunk$set().
  • Inline R code with single backticks embeds computed values directly inside sentences of prose.
  • The same .Rmd can render to HTML, PDF, Word, or slide formats just by changing the output field in the YAML header.
  • Parameterized reports use params in the YAML and rmarkdown::render(params = list(...)) to regenerate a report for different inputs.
  • PDF output requires a LaTeX installation (via tinytex), while HTML output has no such external dependency.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#RProgrammingStudyNotes#RMarkdownBasics#Markdown#Matters#Anatomy#Rmd#StudyNotes#SkillVeris#ExamPrep