My approach takes the long way round, by creating an installer script for all the currently installed packages. It then saves this script, which you can run once you’ve updated R.
Doing it this way accounts for weird behaviour where installing packages from within loops or apply commands doesn’t handle dependencies well and leads to lots of packages failing to install.
This script contains some code from the updateR package, which has never worked for me on its own.
This method can take a while, but in my opinion does a much better job of comprehensively rebuilding your catalogue of libraries
Obviously this only works with CRAN packages, but will spit out a list of packages you need to manually install at the end.
9.2 Update R
9.2.1 Libraries
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(xml2)library(rvest)library(askpass)
9.2.2 Define functions
Most of these are copies or modifications of the updateR functions.
write_package_reinstaller <-function(){# Make a list of currently installed packages packcmds<-NA packages<-list_packages()# Loop through and create an install scriptfor (i in1:length(packages)) { packcmds[i]<-paste("install.packages('",packages[i],"',dependencies = T)",sep ="") }#write installer script to filewrite.table(x = packcmds,file ="packages.install.script.R",quote = F,row.names = F,col.names=FALSE)}
9.2.2.6 update_R
This is a modified version of the update_R function from updateR.
update_R<-function(force=FALSE,ver){ check<-latest_r_version(ver = ver)if(check$update_avail==FALSE){message("Current version is up to date, nothing to be done")}if(check$update_avail==TRUE| force ==TRUE) { admin_password <-ask_password() username <-system('whoami', intern =TRUE) command <-paste0("echo '", admin_password, "' | sudo -S -l") out <-system(command, intern =TRUE)if (length(out) ==0) {stop(sprintf("current user %s does not have admin privileges", username)) } folderpath <-sprintf("/Users/%s/Downloads/",system2("whoami", stdout =TRUE)) pkgfile <-regmatches(check$url, regexpr("R.*$", check$url)) fullpath <-sprintf("%s%s", folderpath, pkgfile)# download package, set folder for downloadmessage("Downloading new version of R")download.file(check$url, fullpath)message("Installinf new version of R") {message(paste0("Installing R-", check$latest, "…please wait")) command <-paste0("echo '", admin_password,"' | sudo -S installer -pkg ","'", fullpath,"'"," -target /")system(command, ignore.stdout =TRUE) arg <-paste0("--check-signature ", fullpath)system2("pkgutil", arg) } }}
9.2.2.7 reinstall_all_packages
reinstall_all_packages <-function(){message("Reinstalling packages from source file")source("packages.install.script.R") new.packages<-list_packages() needinstall<-packages[packages%in%new.packages==FALSE] needinstall<-factor(unique(needinstall))message(needinstall) }
9.2.3 Run the Updater
9.2.3.1 Save the List of packages
#write_package_reinstaller()
9.2.3.2 Find the appropriate version of R for your machine