Maintaining dependencies, code lifecycles, and release procedures are
key responsibilities when developing R packages. devkit
provides modules to automate, audit, and safeguard these workflows.
When onboarding a new developer or setting up a clean machine,
bootstrap_dev_env() automates the installation of standard
R development packages.
# Bootstrap standard development libraries (devtools, testthat, roxygen2, knitr, etc.)
bootstrap_dev_env()Keeping a clean and accurate DESCRIPTION file is
critical for CRAN compliance. devkit provides utilities to
identify missing dependencies and detect unused packages.
audit_dependencies() scans your R/ and
tests/ directories, compares the found namespaces with
those listed in your DESCRIPTION file, and flags
discrepancies.
scan_dependencies() looks at the packages currently
attached or loaded in your R session and compares them to the packages
actually used by a specific script.
# Scan an active script for unused loaded packages
scan_res <- scan_dependencies("scripts/process_data.R")
print(scan_res$unused_packages) # Attached packages not used by the scriptCRAN packages should avoid leaving system directories cluttered.
devkit includes safe package removal utilities.
remove_package() checks if other installed packages
depend on the target package before removing it, preventing broken
dependencies.
remove_user_installed_packages() cleans all
user-installed packages from your library paths while strictly
preserving base and recommended R packages (such as stats,
graphics, survival, Matrix,
etc.).
To prevent pushing broken code, setup_preflight()
installs git pre-commit hooks that validate code styling, documentation,
and tests before a commit is finalized.
# Set up a pre-commit hook that runs checks
setup_preflight(run_docs = TRUE, run_tests = TRUE, run_style = TRUE)Additionally, setup_sentinel() can be used to set up
automated session logging for debugging, writing logs to a file in real
time.
# Initialize session logging at the debug level
setup_sentinel(log_file = "logs/session.log", log_level = "debug")When refactoring a package, you often need to deprecate old
functions. manage_deprecation() automates: 1. Writing a
deprecation warning inside the old function. 2. Creating/updating a
wrapper that points to the new function. 3. Scanning existing scripts,
tests, and vignettes to automatically replace occurrences of the old
function.
# Deprecate an old function in favor of a new one
manage_deprecation(
old_func = "old_calculate_mean",
new_func = "calculate_mean",
wrapper_file = "R/deprecated_wrappers.R",
refactor = TRUE
)When the package is ready for a new release,
architect_release() orchestrates the version bump and
drafts NEWS.md.
And architect_vignette() sets up a new CRAN-compliant
vignette skeleton, inserting all standard headers and boilerplate.