Skip to Content
Course content

35: Building an R Markdown Report

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 airquality dataset using head(). This chunk should show the code (echo=TRUE).
  • Visualization: Create a second code chunk that produces a scatter plot of Ozone vs Temp. To keep the report clean, set this chunk to echo=FALSE so 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.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.