Last active
May 9, 2022 11:13
-
-
Save chrissyhroberts/0b361967f1e46df927704544d7c42fab to your computer and use it in GitHub Desktop.
Updates R and adds back libraries
This file contains 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
#Bare bones updater for R version control. | |
######################################### | |
# Takes the long way round, by creating an installer script for packages. | |
# Accounts for weird behaviour where installing packages from within loops or apply commands doesn't handle | |
# dependencies well an leads to lots of packages failing to install. | |
# This script contains some code from updateR package, which has never worked for me on its own | |
# This can take a while but does a much better job of comprehensively rebuilding your catalogue | |
# Obviously this only works with CRAN packages, but will spit out a list of packages you need | |
# to manually install at the end. | |
# updateR package is here if you want to try | |
# devtools::install_github("AndreaCirilloAC/updateR") | |
library(dplyr) | |
library(xml2) | |
library(rvest) | |
library(askpass) | |
####### GET THE USEFUL FUNCTIONS FROM updateR | |
list_packages <- function() { | |
all_pkg <- installed.packages() %>% | |
as.data.frame() %>% | |
pull(Package) | |
base_pkg <- installed.packages() %>% | |
as.data.frame() %>% | |
filter(Priority == "base") %>% | |
pull(Package) | |
all_pkg[!all_pkg %in% base_pkg] | |
} | |
latest_r_version <- function() { | |
cran <- "http://cran.rstudio.com/bin/macosx/" | |
version_regex <- "(\\d+)?\\.(\\d+)?\\.(\\d+)?" | |
page <- read_html(cran) | |
file_url <- page %>% | |
html_nodes(xpath = "//td/a") %>% .[1] %>% | |
html_attr("href") %>% | |
paste(cran, ., sep = "") | |
minimal <- page %>% | |
html_nodes(xpath = '//table[1]//tr[1]//td[2]') %>% | |
html_text() %>% | |
trimws() %>% | |
regmatches(., regexpr("macOS.*higher.", .)) %>% | |
regmatches(., regexpr("\\d+.\\d+", .)) | |
r_latest <- regmatches(file_url, regexpr(version_regex, file_url)) | |
r_current <- paste(version$major, version$minor, sep = ".") | |
r_latest_numeric <- as.numeric(sub("^(\\d)\\.", "\\1", r_latest)) | |
r_current_numeric <- as.numeric(sub("^(\\d)\\.", "\\1", r_current)) | |
structure( | |
list(update_avail = ifelse(r_latest_numeric > r_current_numeric, T, F), | |
latest = r_latest, | |
url = file_url), | |
current = paste(version$major, version$minor, sep = "."), | |
OS_minimal = as.numeric(minimal) | |
) | |
} | |
ask_password <- function() { | |
askpass::askpass(sprintf("Enter password for %s: ", system2("whoami", stdout = TRUE))) | |
} | |
#Define Chrissy version of this function | |
update_R_chrissy<-function(force=FALSE) | |
{ | |
check<-latest_r_version() | |
if(check$update_avail==FALSE){message("Current version is up to date, nothing to be done")} | |
if(check$update_avail==TRUE | force == TRUE) | |
{ | |
# Make a list of currently installed packages | |
packcmds<-NA | |
packages<-list_packages() | |
# Loop through and create an install script | |
for (i in 1:length(packages)) | |
{ | |
packcmds[i]<-paste("install.packages('",packages[i],"',dependencies = T)",sep = "") | |
} | |
#write installer script to file | |
write.table(x = packcmds,file = "packages.install.script.R",quote = F,row.names = F,col.names=FALSE) | |
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 download | |
message("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) | |
} | |
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) | |
} | |
} | |
update_R_chrissy(force = T) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fixes error that stopped code working on v.4.2.0 update