R
Beginner
2 min read
Introduction to R Markdown Documents
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")