Skip to Content
Course content

94: Practice Exercise: Automating a Weekly Reporting Workflow

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

Think of automating a weekly report like setting up a coffee machine with a programmable timer. You don't want to wake up, manually grind the beans, boil the water, and pour it through a filter every single morning. That's a waste of your mental energy. Instead, you set the parameters once—water level, bean strength, and time—and every morning, you just walk into the kitchen and the coffee is already waiting for you. You've turned a repetitive manual process into a reliable system.

In R, we're doing the exact same thing with your data. Instead of manually opening five different CSVs every Monday morning and copy-pasting them into an Excel sheet, we're building a "machine" that does the following:

  • Gathering the beans: Using list.files() to automatically find all the new data files in a folder without you having to type their names.
  • Brewing the coffee: Using purrr::map_df() or a loop to read and stack those files into one clean data frame.
  • Adding the cream and sugar: Applying dplyr transformations to calculate the weekly KPIs (Key Performance Indicators).
  • Serving the cup: Rendering the final result into a clean PDF or HTML file via R Markdown or Quarto.

Wiring the Pipeline Together

The secret to a workflow that doesn't break is avoiding "hard-coded" values. I can't tell you how many times I've inherited a script where the author wrote read_csv("data_week_42.csv"). The moment week 43 hits, that script is useless. We want to build something that asks, "What is the current date?" and finds the data based on that.

# A snippet of how I usually handle dynamic file loading
library(tidyverse)
library(lubridate)

# Instead of naming a file, we look for everything in the 'weekly_drops' folder
files <- list.files(path = "data/weekly_drops", pattern = "*.csv", full.names = TRUE)

# We read them all and stack them into one giant table in one go
raw_data <- files %>% 
  map_df(~read_csv(.x))

# Now we filter for just the last 7 days based on today's date
weekly_summary <- raw_data %>% 
  filter(report_date >= today() - days(7)) %>% 
  group_by(region) %>% 
  summarize(total_sales = sum(revenue, na.rm = TRUE))

Where This Usually Breaks in Production

Here is a bit of "battle-scarred" advice: the code is rarely where the automation fails. It's almost always the environment. Someone will rename the "weekly_drops" folder to "Weekly_Drops_FINAL", or a teammate will save a CSV with a trailing space in the column name, and your dplyr pipe will suddenly scream at you because revenue doesn't exist anymore.

I highly recommend adding a "sanity check" step. Before you run your heavy analysis, use stopifnot() or a simple if` statement to verify that the folder isn't empty and the columns you expect are actually there. It's much better for the script to crash with a clear message saying "No files found for this week" than to send a blank report to your boss at 8:00 AM.

Closing the Loop with Parameterized Reports

To truly automate, you don't want to run the script and then manually save a plot. You want the script to trigger the report. By using a parameterized R Markdown file, you can pass the "Current Week" as a variable. This allows you to generate reports for any specific time window without ever touching the actual analysis code. You just change one variable at the top of your main controller script, hit 'Run', and walk away.




📋 Practical Task

Exercise: The Weekly E-commerce Performance Pipeline

You have been handed a folder called "sales_data" containing several CSV files named by date (e.g., "2023-10-01.csv", "2023-10-08.csv"). Your task is to build a robust automation script that handles the "Sunday Night Routine" we discussed.

Requirements:

  • Dynamic Loading: Use list.files() to identify all CSVs in the "sales_data" directory.
  • Data Aggregation: Use purrr::map_df() to combine these files into a single data frame called all_sales.
  • The "Last 7 Days" Filter: Use lubridate to filter the data so that only records from the last 7 days (relative to today()) are kept.
  • KPI Calculation: Create a summary table that shows the total_revenue and average_order_value per product_category.
  • The Sanity Check: Implement an if statement that prints a warning message "Warning: No data found for the current window!" if the resulting summary table is empty.

Note: You can simulate the folder by creating 3-4 dummy CSVs with columns report_date, product_category, and revenue before running your automation logic.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.