R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
35: Building an R Markdown Report
When I first started using R Markdown, I fell into a trap that almost every data analyst does: I thought of it as a fancy "Export" button. I would write all my code in a standard R script, run it, and then think, "Okay, now I'll just wrap this in an Rmd file to make it look pretty for my boss."
It's not just a 'Save As' button for your scripts
The misconception is that R Markdown is a formatting tool used after the analysis is done. If you treat it this way, you're missing the entire point of reproducibility. If you write your code in a script, copy-paste the resulting plot into a Word document, and then type your observations manually, you've created a static snapshot. The moment your data source updates or you realize you had a typo in your filtering logic, you have to redo every single manual step. It's a nightmare I've lived through more times than I care to admit.
# The "Wrong Way" Workflow:
# 1. Run script.R
# 2. Save plot_v1.png
# 3. Open Word doc
# 4. Insert plot_v1.png
# 5. Type: "As seen in the plot, the trend is upward."
# 6. Realize the data was wrong. Repeat steps 1-5.
Treating the document as a living recipe
The correct mental model is to view an R Markdown (.Rmd) file as a "living recipe." The code and the narrative live in the same place. When you "Knit" the document, R doesn't just format existing results; it actually executes the code from scratch in a fresh environment and weaves the output directly into the text.
Let's take a real example. Imagine we're analyzing the mtcars dataset to see if vehicle weight actually impacts fuel efficiency. Instead of running a plot and describing it later, we embed the logic. If we decide to filter out cars with more than 8 cylinders, we change one line of code, hit Knit, and the entire report—including the text and the visuals—updates instantly.
Orchestrating YAML and Code Chunks
To make this work, you have to manage three distinct areas of the file. First, the YAML header at the very top. This is the configuration area between the --- lines. It tells R whether you want an HTML page, a PDF, or a Word doc. I usually stick to HTML for internal sharing because it's faster and handles interactive elements better.
Then you have your Markdown text. This is where you explain why you're doing what you're doing. Use headers (#, ##) to structure your argument. This is where you provide the context that a raw script lacks.
Finally, you have the Code Chunks. These are the blocks starting with ```{r}. This is where the heavy lifting happens. A pro tip: use the chunk options to control the output. For example, echo=FALSE is a lifesaver when you want the plot to appear in the final report, but you don't want your manager to have to scroll through twenty lines of ggplot2 code to see it.
---
title: "Fuel Efficiency Analysis"
output: html_document
---
# Analysis of MPG vs Weight
We are investigating if heavier cars consistently get worse mileage.
```{r, echo=FALSE}
plot(mtcars$wt, mtcars$mpg,
main="Weight vs MPG",
xlab="Weight (1000 lbs)",
ylab="Miles per Gallon")
```
As the plot demonstrates, there is a clear negative correlation.
By keeping the code and the commentary together, you've created a document that is self-documenting. Anyone who gets a hold of that .Rmd file can see exactly how you got from the raw data to your final conclusion.
📋 Practical Task
Building a New York Air Quality Trend Report
Your goal is to create a fully reproducible R Markdown report using the built-in airquality dataset. Your report must follow these requirements:
- YAML Configuration: Set the title to "NYC Air Quality Analysis" and the output format to
html_document. - Narrative: Include a top-level header and a paragraph explaining that this report analyzes temperature and ozone levels in New York.
- Data Summary: Create a code chunk that displays the first 6 rows of the
airqualitydataset usinghead(). This chunk should show the code (echo=TRUE). - Visualization: Create a second code chunk that produces a scatter plot of
OzonevsTemp. To keep the report clean, set this chunk toecho=FALSEso the plot is visible, but the code is hidden. - Conclusion: Add a final sentence of text below the plot describing the relationship you see between temperature and ozone.
Save the file with a .Rmd extension and "Knit" it to ensure the HTML renders correctly.
There are no comments for now.