Created
December 16, 2025 16:12
-
-
Save zkamvar/dfbb7593ce0de967a1d44270a1eb9d46 to your computer and use it in GitHub Desktop.
Update development renv profile with default one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Run a renv command with the given profile. | |
| run_profile <- function(profile, code) { | |
| withr::with_envvar(c("RENV_PROFILE" = profile), code) | |
| } | |
| # Synchronize a renv profile with the default. | |
| # | |
| # This will take a renv profile ("dev" by default) and add or update any | |
| # packages that have been added or updated in the default profile. It will not | |
| # remove package that have been added to dev and it will not update any | |
| # packages that are newer in dev. | |
| renv_sync <- function(profile = "dev") { | |
| default <- run_profile("default", renv::lockfile_read()) | |
| dev <- run_profile(profile, renv::lockfile_read()) | |
| run_profile(profile, renv::lockfile_write(default + dev)) | |
| } | |
| # add two renv lockfiles together | |
| # | |
| # This will result in any new packages from profile a added to profile b. This | |
| # does not imply a two-way stream. | |
| `+.renv_lockfile` <- function(a, b) add_renv_lockfile(from = a, to = b) | |
| add_renv_lockfile <- function(from, to) { | |
| pkgs <- list(default = from$Packages, new = to$Packages) | |
| pkg_names <- names(pkgs$default) | |
| # bring in any packages that were added to the default profile | |
| new <- setdiff(pkg_names, names(pkgs$new)) | |
| if (length(new) > 0) { | |
| cli::cli_alert("Adding new packages: {.package {toString(new)}}") | |
| pkgs$new <- c(pkgs$new, pkgs$default[new]) | |
| } | |
| for (pkg in pkg_names) { | |
| dev_pkg <- pkgs$new[[pkg]] | |
| def_pkg <- pkgs$default[[pkg]] | |
| # if there is no change, there is nothing to do | |
| if (identical(dev_pkg, def_pkg)) { | |
| next | |
| } | |
| in_dev <- package_version(dev_pkg$Version) | |
| current <- package_version(def_pkg$Version) | |
| # we skip package updates that are newer in the dev environment | |
| skip_update <- current < in_dev | |
| if (skip_update) { | |
| next | |
| } | |
| # When we reach this point _something_ has changed | |
| # and the package record needs to be updated | |
| cli::cli_alert("Updating {pkg} to {in_dev} -> {current}") | |
| pkgs$new[[pkg]] <- pkgs$default[[pkg]] | |
| } | |
| to$Packages <- pkgs$new[sort(names(pkgs$new))] | |
| to | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment