Run an R Script with Cron
To run an R script on a schedule, call Rscript with the absolute path to your .R file. Rscript runs R non-interactively, which is exactly what cron needs. Use full paths because cron has a minimal environment.
The crontab line
Run crontab -e and add (6 AM daily):
0 6 * * * /usr/bin/Rscript /home/user/analysis/report.R >> /home/user/analysis/report.log 2>&1
Use the absolute path
Find Rscript with which Rscript (often /usr/bin/Rscript or /usr/local/bin/Rscript). Cron's PATH won't include it otherwise, giving 'Rscript: command not found'.
Working directory and libraries
R scripts often assume a working directory. Set it in the script with setwd("/home/user/analysis") or cd first: 0 6 * * * cd /home/user/analysis && /usr/bin/Rscript report.R. If a package isn't found, confirm it's installed for the cron user's library path.
FAQ
How do I run an R script from cron?
0 6 * * * /usr/bin/Rscript /path/report.R runs it daily at 6 AM. Use Rscript (non-interactive R) with the absolute path to the file.
Why do I get 'Rscript: command not found' in cron?
Cron's minimal PATH doesn't include Rscript. Use the absolute path from 'which Rscript', such as /usr/bin/Rscript.
My R script can't find a package in cron. Why?
Packages must be installed in the library path of the user running the cron job. Install them as that user, or set .libPaths() at the top of the script.