Scripts and hooks customize projr’s build process. Scripts control which documents to render; hooks run custom R code before or after builds.
Both are configured in _projr.yml and have separate
settings for production builds (projr_build_patch(), etc.)
and development builds (projr_build_dev()).
Build scripts specify which documents or R scripts projr should render.
Scripts are plain character vectors in _projr.yml. No
sub-keys or nested structures are allowed.
Production builds resolve scripts in this order:
file parameter passed to the build functionbuild.scripts in _projr.yml_quarto.yml or _bookdown.yml project
file.Rmd, .qmd,
.R filesDevelopment builds follow the same order, but check
dev.scripts before falling back to
build.scripts:
file parameterdev.scripts in _projr.ymlbuild.scripts in _projr.yml_quarto.yml or _bookdown.ymlWhen dev.scripts is set, it completely replaces
build.scripts for dev builds. To include production scripts
in dev builds, list them explicitly under dev.scripts.
All specified scripts are validated before the build starts. Missing files cause an immediate error.
Hooks run custom R scripts at specific points in the build process.
Hooks are plain character vectors organized by stage. Three stage
keys are available: pre (before the build),
post (after the build), and both (runs at both
stages). File paths are relative to the project root.
build:
hooks:
pre:
- hooks/setup.R
- hooks/download-data.R
post:
- hooks/cleanup.R
both:
- hooks/log-timestamp.R
dev:
hooks:
pre: hooks/dev-setup.Rbuild.hooks are ignored in dev builds.
dev.hooks are ignored in production builds. Unlike scripts,
there is no fallback between them.
Hooks within a stage run in the order listed. Each hook runs in an
isolated child environment of the global environment, so objects created
in one hook are not available in subsequent hooks. To share data between
hooks, save to disk (e.g., saveRDS()).
Where hooks run in the build flow:
Production build:
1. Pre-build hooks
2. Version bump
3. Clear output directories
4. Git commit (if configured)
5. Render scripts
6. Git commit outputs (if configured)
7. Distribute to remotes
8. Post-build hooks
Development build:
1. Dev pre-build hooks
2. Clear output directories
3. Render scripts
4. Dev post-build hooks
Like scripts, all hook files are validated before the build starts. A missing hook file causes an immediate error. If a hook fails at runtime, the build stops.
validate-credentials.R rather than
hook1.R).tryCatch() so error
messages are actionable.source("hooks/my-hook.R").?projr_yml_hooks_add - Add hooks programmatically?projr_yml_script_add - Add scripts
programmatically?projr_build_dev - Development builds?projr_build_patch - Production builds