Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Last active March 31, 2022 22:03
Show Gist options
  • Save JosiahParry/1b1e511800f4b153e4df07ffa2aeaad1 to your computer and use it in GitHub Desktop.
Save JosiahParry/1b1e511800f4b153e4df07ffa2aeaad1 to your computer and use it in GitHub Desktop.
options("repos" = c("CRAN" = "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"))
install.packages("pak")
# List of R packages to install
to_install <- readLines("all-pkgs.txt")
# copy of purrrs safely function for system requirements.
# this way we can capture errors associated with packages that
# wont be in paks database
capture_error <- function (code, otherwise = NULL, quiet = TRUE) {
tryCatch(list(result = code, error = NULL), error = function(e) {
if (!quiet)
message("Error: ", e$message)
list(result = otherwise, error = e)
}, interrupt = function(e) {
stop("Terminated by user", call. = FALSE)
})
}
safely <- function(.f, otherwise = NULL, quiet = TRUE) {
function(...) capture_error(.f(...), otherwise, quiet)
}
safe_sysreqs <- safely(pak::pkg_system_requirements)
# find package system requirements
sysreqs <- lapply(to_install, safe_sysreqs)
# find only the unique system requirements
sysreq_cmds <- unique(unlist(lapply(sysreqs, function(x) x[["result"]])))
# write to bash file
writeLines(c(
"#!/bin/bash", # make executable
# install build-essential first
"sudo apt-get install build-essential",
sysreq_cmds, # then add the requirements
""), # add empty line at the end
"install-sysreqs.sh")
# install sysdeps
system("./install-sysreqs.sh")
# install all packages
install.packages(to_install, dependencies = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment