Useful git pre-commit hooks for R

CRAN status Lifecycle: experimental

R build status

Pre-commit hooks are tests that run each time you attempt to commit. If the tests pass, the commit will be made, otherwise not. A very basic test is to check if the code is parsable, making sure you have not forgotten a comma, brace or quote.

The goal of this package is to twofold:


You can install the package from CRAN:


To access pre-commit functionality from R, you also need to install the pre-commit framework. The following command line methods are tested to work with this R package:

Alternatively, you can handle the installation from R using miniconda:

Then, in a fresh R session:

# once in every git repo either
# * after cloning a repo that already uses pre-commit or
# * if you want introduce pre-commit to this repo

The last command initializes pre-commit in your repo and performs some set-up tasks like creating the config file .pre-commit-config.yaml, where the hooks that will be run on git commit are specified. See ?precommit::use_precommit() to see how you can use a custom .pre-commit-config.yaml instead of the default at initialization. You can (obviously) change edit the file manually at any time.


The next time you run git commit, the hooks listed in your .pre-commit-config.yaml will get executed before the commit. The helper function precommit::open_config() let’s you open and edit the .pre-commit-config.yaml conveniently from the RStudio console. When any file is changed due to running a hook or the hook script errors, the commit will fail. You can inspect the changes introduced by the hook and if satisfied, you can add the changes made by the hook to the index with git add path/to/file and attempt to commit again. Some hooks change files, like the styler hook, so all you need to do to make the hook pass is git add the changes introduced by the hook. Other hooks, like the parsable-R hook, will need your action, e.g. add a missing closing brace to a call like library(styler, before they pass at the next attempt. If all hooks pass, the commit is made. You can also temporarily disable hooks. If you succeed, it should look like this:

See the hooks provided by this repo under vignette("available-hooks"). You can also add other hooks from other repos, by extending the .pre-commit-config.yaml file, e.g. like this:

-   repo:
    rev: v1.2.3
    -   id: check-added-large-files

To update the hook revisions, run precommit::autoupdate().


# > Error in loadNamespace(name) : there is no package called ‘__name_of_package__’

In that case, just install the package with install.packages() once and try to commit again. All R dependencies of the pre-commit hooks provided in this repo are suggested dependencies of this R package, so if you want to install them all, just install.packages("precommit", dependencies = c("Imports", "Suggests")).


The online documentation of this package only covers the functionality added on top of pre-commit by this package. Everything else is covered in the extensive online documentation of the pre-commit framework itself, including how to:


uninstall_precommit("repo") # just for the repo you are in.
uninstall_precommit("user") # remove the pre-commit conda executable.