Pre-Commit Hook for Processing README.Rmd

When writing an R package I usually create a README.Rmd file that I render to README.md. I use {pkgdown} to then create documentation. I run the last step via CI, so once it’s set up I never need to think about it again.

The problem is that I regularly forget to process the README.Rmd file, which means that despite keeping that up to date, everything else lags behind.

What if I automated the process? I created a simple pre-commit hook which processes README.Rmd whenever I make a commit and automatically adds any changes to the commit.

To do this is simple: just create a file called pre-commit in the .git/hooks folder with the following content:

#!/bin/sh

if [ README.Rmd -nt README.md ] || [ ! -f README.md ]
then
        R -e "rmarkdown::render('README.Rmd')"
        git add README.md
fi

Make it executable and… Voila! 🚀 Whenever you commit, the README.md file will be updated if it’s older than the most recent change to README.Rmd. If README.md doesn’t exist, then it will be created.