package_add()
Add a package to the project
Records a package in the project's settings (in settings/packages.yml when the project uses split settings files, otherwise in settings.yml) and installs it if it isn't installed yet. Recording the package is what makes it reproducible: scaffold() and packages_install() install everything listed, and auto-attached packages are loaded by scaffold().
Usage
package_add(package, auto_attach = FALSE, install = TRUE)
Arguments
| Name | Default | Description |
|---|---|---|
| package |
Package specification: |
|
| auto_attach | FALSE |
Logical. Whether |
| install | TRUE |
Logical. Install the package now if it isn't installed (or
the installed version doesn't match a pin). Default TRUE. If the install
fails, the settings are left as they were and |
Value
Invisibly, the package entry that was recorded.
Details
Adding a package that is already listed replaces its entry, so this is also how to pin a version or turn auto-attach on or off.
Afterward, the Installed Packages section of the project's AI context file (ai.canonical_file, else AGENTS.md, else CLAUDE.md) is rewritten from the package settings, so agents see the new package. Only the section marked <!-- @framework:regenerate --> changes; remove the marker to opt out.
Examples
Not run
if (FALSE) {
package_add("janitor")
package_add("forcats", auto_attach = TRUE)
package_add("tidyverse/dplyr@main")
package_add("dplyr@1.1.0")
}