R Markdown is a powerful tool that combines the simplicity of Markdown with the computational power of R. It enables you to:
🎯 Key Benefits: - Create reproducible documents that combine narrative text and code - Generate professional reports in multiple formats (HTML, PDF, Word) - Build interactive dashboards and presentations - Share your work with both technical and non-technical audiences
Perfect for: - 📊 Data Analysis Reports - 📚 Technical Documentation - 🎓 Academic Papers - 📑 Presentations - 🔍 Exploratory Data Analysis
An R Markdown document consists of three essential components, each serving a specific purpose:
The YAML header controls document-wide settings and output format:
---
title: "My First R Markdown" # Document title
author: "Your Name" # Author name
date: "2024-03-20" # Publication date
output: html_document # Output format (html, pdf, word)
---Key YAML Options: - title: Main
document heading - author: Document creator -
date: Publication date (can use “2025-02-14” for automatic
dating) - output: Desired output format
Markdown syntax provides easy-to-use formatting:
Text Formatting: - Italics: Surround text
with single asterisks (*text*) - Bold: Use
double asterisks (**text**) - Code: Use
backticks for inline code (`code`) - Links: Create with
[text](url)
Lists: - Unordered lists use hyphens (-) * Sub-items use indentation + Can use different markers - Continue main list
Three methods to insert code chunks:
Cmd + Option + ICtrl + Alt + IControl chunk behavior with options:
# Load required packages
library(tidyverse)
# Create an example plot
ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) +
geom_point(size = 3) + # Add points
labs(title = "Car Weight vs MPG", # Add labels
x = "Miles Per Gallon",
y = "Weight (1000 lbs)",
color = "Cylinders") +
theme_minimal() # Use minimal themeEssential Chunk Options: - echo:
Show/hide code (TRUE/FALSE) - eval: Execute code
(TRUE/FALSE) - message: Display messages (TRUE/FALSE) -
warning: Show warnings (TRUE/FALSE) -
fig.width/fig.height: Set plot dimensions -
fig.cap: Add figure caption - include: Include
chunk output (TRUE/FALSE)
📝 Structure Guidelines:
✅ Recommended: - Begin with clear introduction - Use logical heading hierarchy - Include navigation aids (TOC) - End with summary/conclusions - Keep sections focused
❌ Avoid: - Skipping heading levels - Writing walls of text - Leaving code unexplained - Mixed formatting styles
🔧 Code Organization:
✅ Best Practices:
📚 Writing Guidelines:
✅ Do: - Use consistent formatting - Break text into paragraphs - Include descriptive lists - Add relevant references
❌ Don’t: - Mix formatting styles - Write dense paragraphs - Leave terms undefined - Skip documentation
🎨 Presentation Tips:
# Create a professional visualization
ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) +
geom_point(size = 3, alpha = 0.7) + # Add points with transparency
geom_smooth(method = "lm", se = TRUE) + # Add trend line with CI
facet_wrap(~cyl, scales = "free") + # Split by cylinders
labs(
title = "Car Weight vs MPG by Number of Cylinders",
subtitle = "Analysis of Motor Trend Car Road Tests",
x = "Miles Per Gallon (MPG)",
y = "Weight (1000 lbs)",
color = "Cylinders"
) +
theme_minimal() + # Use minimal theme
theme(
legend.position = "bottom", # Move legend
plot.title = element_text(size = 16), # Customize text
plot.subtitle = element_text(size = 12)
)## `geom_smooth()` using formula = 'y ~ x'
Detailed Analysis of MPG vs Weight
# Create a formatted table
library(knitr)
mtcars %>%
head() %>% # Take first 6 rows
select(mpg, cyl, wt, hp) %>% # Select key columns
kable(
caption = "Sample of Motor Trend Car Data",
digits = 2, # Round numbers
align = c('r', 'c', 'r', 'r') # Align columns
)| mpg | cyl | wt | hp | |
|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 2.62 | 110 |
| Mazda RX4 Wag | 21.0 | 6 | 2.88 | 110 |
| Datsun 710 | 22.8 | 4 | 2.32 | 93 |
| Hornet 4 Drive | 21.4 | 6 | 3.21 | 110 |
| Hornet Sportabout | 18.7 | 8 | 3.44 | 175 |
| Valiant | 18.1 | 6 | 3.46 | 105 |
R Markdown supports LaTeX equations:
Inline Math: \(y = \beta_0 + \beta_1x + \epsilon\)
Display Math: \[ \text{MPG} = \beta_0 + \beta_1\text{Weight} + \beta_2\text{Cylinders} + \epsilon \\ \text{where } \epsilon \sim N(0, \sigma^2) \]
External images can be included:
🔍 Common Issues and Solutions:
🎯 Hands-on Learning:
🚀 Advanced Topics to Explore:
📚 Learning Materials: