SyntaxStudy
Sign Up
R Introduction to R Markdown Documents
R Beginner 2 min read

Introduction to R Markdown Documents

R Markdown is a file format that weaves together narrative text written in Markdown, code chunks written in R (or Python, SQL, Bash, and others), and their output into a single reproducible document. The rmarkdown package, backed by the knitr engine and the Pandoc universal document converter, can render an .Rmd file into HTML, PDF, Word, slides, dashboards, websites, and books. The core promise is reproducibility: anyone with the same data and code can regenerate the exact same output, from figures to tables to p-values. An R Markdown document begins with a YAML front matter block delimited by triple dashes that specifies metadata such as the title, author, date, and output format. The output field determines the target format — html_document, pdf_document, word_document, beamer_presentation, ioslides_presentation, and many more. The YAML block can also contain nested options for the output renderer, such as table of contents settings, theme, CSS, LaTeX packages, and numbered sections. Code chunks are delimited by triple backticks with {r} (or another language name). Each chunk can have named options that control its behaviour: echo = FALSE hides the source code while showing output, eval = FALSE shows the code but does not run it, include = FALSE runs the code but hides both code and output, warning = FALSE and message = FALSE suppress the corresponding R messages, and fig.width / fig.height control figure dimensions. Global defaults for all chunks are set once in a setup chunk at the top of the document.
Example
# Contents of a typical R Markdown file (example.Rmd)
# -------------------------------------------------------

# --- YAML Front Matter ---
# ---
# title: "Sales Analysis Report"
# author: "Jane Smith"
# date: "`r Sys.Date()`"
# output:
#   html_document:
#     toc: true
#     toc_float: true
#     theme: flatly
#     code_folding: hide
#   pdf_document:
#     toc: true
#     number_sections: true
# ---

# --- Setup chunk (invisible, sets global options) ---
# ```{r setup, include=FALSE}
# knitr::opts_chunk$set(
#   echo    = TRUE,
#   warning = FALSE,
#   message = FALSE,
#   fig.width  = 7,
#   fig.height = 4,
#   dpi        = 150
# )
# library(ggplot2)
# library(dplyr)
# ```

# --- Regular prose ---
# ## Introduction
# This report summarises Q1 sales data.
# We found that revenue grew by **12%** year-over-year.

# --- Data chunk ---
# ```{r load-data}
# sales <- read.csv("sales_q1.csv")
# head(sales)
# ```

# --- Figure chunk ---
# ```{r revenue-plot, fig.cap="Monthly revenue trend"}
# ggplot(sales, aes(x = month, y = revenue)) +
#   geom_col(fill = "steelblue") +
#   theme_minimal()
# ```

# --- Inline R code in text ---
# The mean revenue was `r round(mean(sales$revenue), 0)` dollars.

# Rendering from R console:
# rmarkdown::render("example.Rmd")
# rmarkdown::render("example.Rmd", output_format = "pdf_document")