Skip to content

Instantly share code, notes, and snippets.

@JBGruber
Last active March 16, 2025 17:56
Show Gist options
  • Save JBGruber/f751d414ced2374b9856451531c5997d to your computer and use it in GitHub Desktop.
Save JBGruber/f751d414ced2374b9856451531c5997d to your computer and use it in GitHub Desktop.
Update all R packages using pak

Most efficient way to bring all packages on your system up to the newes version. Use with

source("https://gist.githubusercontent.com/JBGruber/f751d414ced2374b9856451531c5997d/raw/yay.r")
yay()

Limitation

Only update CRAN packages due to the usage of old.packages(). Let me know if you have a better way to find outdated packages.

#' Update all packages using pak
#'
#' @details
#' Queries old packages held in your package library and updates them.
#'
#'
#' @param lib Package library to install the packages to. Note that _all_
#' dependent packages will be installed here, even if they are already
#' installed in another library. The only exceptions are base and recommended
#' packages installed in `.Library`. These are not duplicated in `lib`, unless
#' a newer version of a recommended package is needed.
#' @param upgrade When `FALSE`, the default, pak does the minimum amount of work
#' to give you the latest version(s) of `pkg`. It will only upgrade dependent
#' packages if `pkg`, or one of their dependencies explicitly require a higher
#' version than what you currently have. It will also prefer a binary package
#' over to source package, even it the binary package is older.
#'
#' When `upgrade = TRUE`, pak will ensure that you have the latest version(s)
#' of `pkg` and all their dependencies.
#' @param ask Whether to ask for confirmation when installing a different
#' version of a package that is already installed. Installations that only add
#' new packages never require confirmation.
#' @param dependencies What kinds of dependencies to install. Most commonly one
#' of the following values:
#' - `NA`: only required (hard) dependencies,
#' - `TRUE`: required dependencies plus optional and development
#' dependencies,
#' - `FALSE`: do not install any dependencies. (You might end up with a
#' non-working package, and/or the installation might fail.) See [Package
#' dependency types] for other possible values and more information about
#' package dependencies.
#' @return (Invisibly) A data frame with information about the installed
#' package(s).
#' @param static_v8 automatically download a static version of libv8 (for V8
#' package)
#'
#' @export
#'
#' @examples
yay <- function(lib = .libPaths()[[1L]], upgrade = TRUE,
ask = interactive(), dependencies = NA,
static_v8 = TRUE) {
# check dependencies of this function
if (!require("renv")) install.packages("renv")
rlang::check_installed(c("cli", "pak", "withr"))
pkg <- old.packages()[, "Package"]
# V8 is a pain to compile and is not handled well by pak, setting
# DOWNLOAD_STATIC_LIBV8 uses a binary version of libv8, speeding things up
if ("V8" %in% pkg) {
cli::cli_alert_info("Package V8 needs an update, using `install.packages` for this one")
withr::with_envvar(
new = c("DOWNLOAD_STATIC_LIBV8" = static_v8),
code = install.packages("V8")
)
pkg <- unique(setdiff(pkg, "V8"))
}
inst <- pak::pkg_install(pkg = pkg, lib = lib, upgrade = upgrade,
ask = ask, dependencies = dependencies)
invisible(inst)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment