R
R packages
Best practices
Foundations series · part 4

Pre-commit hooks, formatting & linting

Let machines argue about spaces so reviewers can talk about logic

Catch syntax errors, stale documentation, and styling differences automatically before code reaches review.
Published

2026-09-26

Modified

2026-09-27

The problem

Half the comments on an unformatted PR are about style: spacing, <- vs =, line length. Those are cheap for a machine to fix and expensive for a person to review. Then there are the embarrassing slips: a browser() left in, a file that doesn’t parse, a NAMESPACE out of date.

Three layers solve this. Each one is faster and closer to you than the next:

Layer Runs Catches
Editor (format on save) While you type Style
Pre-commit hook On git commit, on staged files Style, lint, debug leftovers, unparsable files
CI On every PR Everything, for everyone, with no way to skip

The web-repo way

In our TypeScript project, husky installs git hooks when you run npm install, and lint-staged runs the formatter and linter (Biome) on staged files only:

// lint-staged.config.mjs
export default {
  "*.{ts,tsx,js,json,css,md}": ["biome check --write --no-errors-on-unmatched"],
};

Design choices worth copying:

  • Only staged files, so a commit takes about a second, however big the repo.
  • Auto-fix, then re-stage. Formatting is fixed, not just reported.
  • Slow checks stay in CI. Typecheck, tests and build run on the PR, not on every commit. If a hook is slow, people learn to skip it with --no-verify.
  • CI re-runs the same checks (biome ci), because hooks are local and optional.

The R way

Formatter: Air (or styler)

Air is Posit’s formatter for R. It’s extremely fast, and it’s built into Positron and available for RStudio. styler is the long-standing alternative.

usethis::use_air()   # usethis >= 3.2.0: configures the project for Air
air format .          # format the project
air format . --check  # fail if anything would change (for CI)

Linter: lintr

lintr::use_lintr(type = "tidyverse")   # writes a .lintr config
lintr::lint_package()

The linter finds what a formatter can’t: unused variables, T/F instead of TRUE/FALSE, 1:length(x), object names, cyclomatic complexity.

Pre-commit hooks: the precommit package

precommit brings the pre-commit framework to R, with hooks written for R projects. Save templates/pre-commit/pre-commit-config.yaml in your project root as .pre-commit-config.yaml:

repos:
  - repo: https://github.com/posit-dev/air-pre-commit
    rev: 0.11.0
    hooks:
      - id: air-format
  - repo: https://github.com/lorenzwalthert/precommit
    rev: v0.4.3
    hooks:
      - id: parsable-R
      - id: no-browser-statement
      - id: no-debug-statement
      - id: roxygenize          # keep man/ and NAMESPACE in sync
      - id: deps-in-desc        # every pkg::fn() is declared in DESCRIPTION
      - id: lintr
      - id: readme-rmd-rendered
  - repo: https://github.com/gitleaks/gitleaks   # see secret scanning
    rev: v8.30.1
    hooks:
      - id: gitleaks
precommit::use_precommit()   # writes the config (if missing) and installs the git hook

It relies on the Python pre-commit tool. precommit::install_precommit() can set it up for you.

CI: the gate nobody can skip

r-lib/actions includes lint.yaml, lint-changed-files.yaml and style.yaml examples, and Air has posit-dev/setup-air:

usethis::use_github_action("lint")   # lintr on every PR
usethis::use_github_action(url = "https://github.com/posit-dev/setup-air/blob/main/examples/format-check.yaml")

Air’s format-suggest.yaml example goes one better. It posts the formatting fixes as suggestions on the PR, so a contributor without Air can accept them with a click.

Pitfalls

  • Format the whole repo once, in its own PR, before switching the check on. Otherwise the first real PR drowns in unrelated diffs.
  • Hooks aren’t cloned. Every developer runs precommit::use_precommit() (or npm install) once. Put it in CONTRIBUTING.
  • Keep hooks fast. roxygenize is the slow one in big packages; move it to CI if people start skipping hooks.
  • Pin hook versions (rev:). Dependabot’s pre-commit ecosystem can keep them current.
  • Don’t let the hook and CI disagree. Use the same tool versions and config files for both.

Sources

Web-repo tools

R tools

R package documentation

NoteUpdates
  • 2026-09-26: Removed the end-of-post checklist.
  • 2026-09-27: Added a Sources section.
  • 2026-09-27: Moved the pre-commit configuration into a reusable template.