Skip to Content
Course content

144: Automating Report Generation with cron and R

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

By now, you're probably comfortable writing R Markdown reports that look great. But running rmarkdown::render() manually every morning is a waste of your time. The real power comes when you treat your R scripts as background services. Since we're working in a Unix-like environment, we're going to use cron—the standard time-based job scheduler.

Creating the Report Wrapper

I usually don't put my render() call directly inside the .Rmd file (obviously) or run the .Rmd via a command line flag. Instead, I write a small "wrapper" R script. This gives me a place to handle environment variables, log the start time, or check if the source data actually exists before triggering the heavy lift of rendering.

# run_daily_report.R
library(rmarkdown)

# I'm using a fixed path here because cron is picky about working directories
report_path <- "/home/dev/reports/daily_health_check.Rmd"
output_file <- "/home/dev/reports/daily_health_check.html"

# Simple check to ensure we aren't rendering an empty dataset
if (file.exists("/home/dev/data/system_logs.csv")) {
  render(report_path, output_file = output_file)
  cat("Report generated successfully at", as.character(Sys.time()), "\n")
} else {
  stop("Source data missing! Report aborted.")
}

Scheduling the Job with crontab

To tell the system to run this, we use the crontab -e command. This opens a text file where each line represents a scheduled task. I want this report to run every morning at 6:00 AM, so I'll use the 0 6 * * * syntax.

Here is how I first wrote my entry:

0 6 * * * Rscript run_daily_report.R

The "Where is R?" Mistake

I'll be honest: I still make this mistake occasionally. I saved the crontab, waited for the trigger, and... nothing happened. No report, no error email. When I checked the system logs, it turns out cron has a very minimal PATH. It doesn't know where Rscript lives, and it definitely doesn't know that my script is located in /home/dev/reports/.

When you're running a script manually in your terminal, your shell knows your home directory and where your binaries are. cron does not. To fix this, I need to provide the absolute path to the R executable and the absolute path to the script.

First, I find my R path by running which Rscript in the terminal, which gives me /usr/local/bin/Rscript. Then, I update the crontab:

0 6 * * * /usr/local/bin/Rscript /home/dev/reports/run_daily_report.R >> /home/dev/reports/cron_log.log 2>&1

I added >> /home/dev/reports/cron_log.log 2>&1 to the end. This is a lifesaver—it redirects both the standard output and any error messages into a log file. Without this, if your R script crashes, the error vanishes into the void, and you're left guessing why the report didn't generate.

Verifying the Automation

Since I can't wait until 6:00 AM to see if this worked, I'll set it to run one minute from now. If it's currently 2:15 PM, I'll set it to 16 16 * * *. I'll then tail the log file in my terminal to watch it happen in real-time:

tail -f /home/dev/reports/cron_log.log

Once I see the "Report generated successfully" message pop up, I know the plumbing is correct. I can then switch the timing back to the actual production schedule and stop worrying about it.




📋 Practical Task

Build an Automated Weekly Website Traffic Digest

You have been asked to automate a weekly traffic report. You have an R Markdown file located at /home/analyst/projects/traffic_report.Rmd and a data file at /home/analyst/data/web_stats.csv.

Your task is to create the following two components:

  1. The Wrapper Script: Write an R script (run_traffic_report.R) that checks if web_stats.csv exists. If it does, use rmarkdown::render() to create an HTML output at /home/analyst/projects/weekly_digest.html.
  2. The Cron Entry: Write the exact line you would add to the crontab to run this script every Monday at 8:00 AM. Assume the Rscript binary is located at /usr/bin/Rscript and the wrapper script is in /home/analyst/projects/. Ensure you include a log redirect to /home/analyst/projects/traffic_log.txt.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.