Site icon R-bloggers

Pre-Commit Hook for Processing README.Rmd

[This article was first published on R | datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

When writing an R package I usually write 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 I never need to think about it.

The problem is that I regularly forget to process the README.Rmd file, which means that despite keeping that it 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 ]
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.

To leave a comment for the author, please follow the link and comment on their blog: R | datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.