R Markdown and Quarto in R

📘 Introduction

R Markdown and Quarto are powerful tools for creating dynamic documents that combine text, R code, tables, visualizations, and results in a single file. They support reproducible research by automatically updating outputs whenever the source code changes. Documents can be rendered into formats such as HTML, PDF, Word, presentations, websites, and books.

Information

R Markdown is based on the rmarkdown package, while Quarto is a modern scientific and technical publishing system that supports R, Python, Julia, and Observable JavaScript.

đŸŽ¯ Why Learn R Markdown and Quarto?

  • Create reproducible reports.
  • Combine code, results, and explanations in one document.
  • Generate professional reports automatically.
  • Produce presentations, websites, and books.
  • Share data analysis in a consistent and reproducible manner.

📚 R Markdown vs Quarto

FeatureR MarkdownQuarto
Primary LanguageR.R, Python, Julia, and more.
File Extension.Rmd.qmd
Cross-Language SupportLimited.Built-in.
PublishingReports and presentations.Reports, books, websites, dashboards, and presentations.
Recommended for New ProjectsExisting workflows.Yes.

đŸ“Ļ Installing Required Packages

Install Required Packages

install.packages("rmarkdown")
install.packages("knitr")

library(rmarkdown)
library(knitr)

📄 Basic Structure of an R Markdown Document

An R Markdown document consists of a YAML header, Markdown text, and executable R code chunks.

Simple R Markdown Document

---
title: "Sales Report"
author: "John Doe"
date: "2026-07-09"
output: html_document
---

# Introduction

This report analyzes monthly sales.

```{r}
summary(cars)
```

📄 Basic Structure of a Quarto Document

Quarto uses a similar structure but includes a simplified YAML configuration.

Simple Quarto Document

---
title: "Sales Report"
format: html
---

# Introduction

This report analyzes monthly sales.

```{r}
summary(cars)
```

📝 Markdown Syntax

SyntaxPurpose
# HeadingLevel 1 heading.
## HeadingLevel 2 heading.
**Bold**Bold text.
*Italic*Italic text.
- ItemBullet list.
1. ItemNumbered list.
[Text](URL)Hyperlink.

đŸ’ģ R Code Chunks

Code chunks contain executable R code enclosed within triple backticks.

R Code Chunk

```{r}
x <- 10
y <- 20

x + y
```

âš™ī¸ Chunk Options

Chunk options control how code and results appear in the output document.

OptionDescription
echoDisplay the R code.
evalExecute the code.
warningDisplay warning messages.
messageDisplay informational messages.
fig.widthFigure width.
fig.heightFigure height.

Chunk Options Example

```{r echo=FALSE, warning=FALSE}
summary(iris)
```

📊 Including Tables

Create a Table

library(knitr)

kable(
  head(iris),
  caption = "Sample Iris Dataset"
)

📈 Including Charts

Charts generated inside code chunks automatically appear in the rendered document.

Plot Example

```{r}
plot(
  iris$Sepal.Length,
  iris$Sepal.Width,
  col = "blue",
  pch = 19
)
```

📑 Generating Reports

R Markdown and Quarto documents can be rendered into multiple output formats.

FormatExample
HTMLInteractive web report.
PDFPrintable report.
WordMicrosoft Word document.
PresentationSlides.
WebsiteStatic website.

📤 Rendering Documents

Render an R Markdown File

library(rmarkdown)

render(
  "report.Rmd"
)

Render a Quarto Document

quarto render report.qmd

📊 Parameters in R Markdown

Parameters allow reports to be generated dynamically using different input values.

Parameterized Report

---
title: "Sales Report"
params:
  year: 2026
output: html_document
---

```{r}
params$year
```

🎨 Quarto Callouts

Quarto supports built-in callouts for highlighting important information.

Quarto Callout

::: {.callout-note}
This report was generated automatically using Quarto.
:::

📖 Creating Presentations

Both R Markdown and Quarto can generate presentations using formats such as Reveal.js and Beamer.

Reveal.js Presentation

---
title: "Sales Presentation"
format: revealjs
---

# Sales Summary

- Revenue increased.
- Expenses decreased.
- Profit improved.

🌐 Creating Websites

Quarto can generate complete websites from multiple documents.

Website Configuration

project:
  type: website

website:
  title: "My Data Science Website"

🌍 Real-World Example

A data analyst prepares a monthly business report that includes descriptive statistics, visualizations, and predictive models. The report is written in Quarto so that every month, updated data automatically produces a new HTML report with refreshed tables and charts.

Monthly Business Report

---
title: "Monthly Sales Report"
format: html
---

# Sales Summary

```{r}
summary(iris)

plot(
  iris$Sepal.Length,
  iris$Petal.Length
)
```

🔄 Report Generation Workflow

Write Document
Add Markdown Content
Insert Code Chunks
Run Analysis
Generate Tables and Charts
Render Final Report
Share Output

📋 Common Functions and Commands

Function/CommandPurpose
render()Renders an R Markdown document.
kable()Creates formatted tables.
quarto renderRenders a Quarto document.
echoControls code visibility.
evalControls code execution.
fig.widthSets figure width.
fig.heightSets figure height.

âš ī¸ Common Mistakes

MistakeExplanationSolution
Using incorrect YAML syntaxDocuments may fail to render.Ensure proper indentation and formatting.
Forgetting to install required packagesRendering may fail due to missing dependencies.Install and load necessary packages before rendering.
Leaving unnecessary code visibleReports become cluttered.Use echo=FALSE to hide implementation details.
Hardcoding valuesReports become difficult to reuse.Use parameters for dynamic report generation.

💡 Best Practices

  • Use Quarto for new projects that may involve multiple programming languages.
  • Write clear Markdown explanations alongside code.
  • Organize reports with meaningful headings and sections.
  • Hide unnecessary code while displaying important results.
  • Use parameters to generate reusable reports.
  • Include tables and visualizations to improve readability.
  • Test rendering regularly throughout development.

Best Practice

R Markdown and Quarto promote reproducible research by combining analysis, documentation, and results into a single source file. Quarto extends these capabilities with broader language support, flexible publishing options, and modern document features, making it an excellent choice for new analytical and reporting projects.

📝 Summary

R Markdown and Quarto enable the creation of dynamic, reproducible documents that integrate text, code, tables, and visualizations. In this chapter, you learned the structure of R Markdown and Quarto documents, Markdown syntax, code chunks, chunk options, table generation, chart integration, rendering documents, parameterized reports, presentations, websites, and report publishing. Mastering these tools allows you to automate report generation, improve reproducibility, and communicate analytical results professionally across multiple output formats.

>>"Reproducible reports combine code, analysis, and documentation into a single source of truth, making research transparent, efficient, and easy to share."