Skip to content

Instantly share code, notes, and snippets.

@dave-mills
Created October 21, 2024 14:37
Show Gist options
  • Save dave-mills/9562745ab5994d614960c86d6ec264b4 to your computer and use it in GitHub Desktop.
Save dave-mills/9562745ab5994d614960c86d6ec264b4 to your computer and use it in GitHub Desktop.
A proper "renv update" script
#####################################
## This script will install all the packages listed in the renv.lock file.
## It is different to `renv::restore()`, because it will install the latest available versions of each package, rather than the versions that were used when the lockfile was created
##
## This is equivalent to a 'composer update' in php (while renv::restore() is equivalent to 'composer install').
##
## Written because `renv::update()` only updates packages that are currently installed, so is not helpful when the versions specified in a lock file no longer install properly.
#####################################
install.packages("jsonlite")
library(jsonlite)
# Load the renv lock file
renv_packages <- jsonlite::fromJSON("renv.lock")
# get the keys of the list of packages
package_names <- names(renv_packages$Packages)
# install the packages (this will take a while)
install.packages(package_names)
# update the .lock file with the latest versions
renv::snapshot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment