# Thesis-It analysis record: install-packages.R # Copyright (C) 2026 Thesis-It. # # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) # any later version. It is distributed WITHOUT ANY WARRANTY. COPYING holds # the licence's text. # # It installs the R packages this record needs to rerun its analyses and # read its files, and the packages they need to build and to run, # each at the version packages.csv lists and from the exact source it names. # It checks packages.csv by its SHA-256, then downloads every archive and # checks each one's SHA-256 against packages.csv before it installs any of # them. It installs them into a folder of this computer's own, in R's user # data folder, kept apart for this list, this R and its platform, and says # where. rerun.R reads them from there alone. It never installs beside the # record, and stops where the record's folder holds R packages, a startup # file R reads (.Rprofile or .Renviron) or a link to another file, none of # which a record carries. # # Rscript --vanilla install-packages.R # # --vanilla keeps R from reading startup files, such as an .Rprofile beside # this file, which could change what it does. It needs R 4.5.0 or later, for # tools::sha256sum, a connection to the addresses packages.csv names, and # what building R packages from source needs: C, C++ and Fortran compilers, # make, and, for haven, zlib's headers (zlib1g-dev on Debian and Ubuntu). # It writes only into that # folder, and downloads the archives into a temporary folder it removes. .thesisit_install <- local({ R_VERSION <- "4.6.0" R_MINIMUM <- "4.5.0" PACKAGES_SHA256 <- "f3a8915f219e5271ba4d6b7793d13aeeeefe2c11cf14f033fb56a3b8c6d6e65b" COLUMNS <- c("package", "version", "source", "sha256", "license", "in_compiled_code", "compiles_own_code") packages_home <- function() { version <- paste(R.version$major, R.version$minor, sep = ".") file.path(tools::R_user_dir("thesisit", "data"), "record-packages", PACKAGES_SHA256, paste0("R-", version, "-", R.version$platform)) } stamp <- function() { c(PACKAGES_SHA256, paste("R", paste(R.version$major, R.version$minor, sep = "."), R.version$platform)) } package_fault <- function(name, version, library, compiles) { found <- suppressWarnings(utils::packageDescription(name, lib.loc = library, fields = c("Version", "Built"))) there <- inherits(found, "packageDescription") && is.character(found$Version) if (!there || !identical(found$Version, version)) return(paste0(name, " ", version, " is not installed for this R", if (there) paste0(" (", name, " ", found$Version, " is)") else "")) built <- trimws(strsplit(if (is.character(found$Built)) found$Built else "", ";", fixed = TRUE)[[1L]]) r <- paste("R", paste(R.version$major, R.version$minor, sep = ".")) if (length(built) < 2L || !identical(built[1L], r)) return(paste0(name, " ", version, " was built by ", if (length(built) > 0L && nzchar(built[1L])) built[1L] else "another R", ", not by this ", r)) if (!(built[2L] %in% c("", R.version$platform))) return(paste0(name, " ", version, " was built for ", built[2L], ", not for this computer's ", R.version$platform)) if (compiles && !identical(built[2L], R.version$platform)) return(paste0(name, " ", version, " has no code built for this computer's ", R.version$platform)) NULL } planted <- function(here) { root <- normalizePath(here, winslash = "/", mustWork = TRUE) full <- function(path) normalizePath(path, winslash = "/", mustWork = FALSE) within <- function(path) identical(full(path), root) || startsWith(full(path), paste0(root, "/")) shown <- function(path) if (startsWith(full(path), paste0(root, "/"))) substring(full(path), nchar(root) + 2L) else full(path) entries <- list.files(here, all.files = TRUE, no.. = TRUE) linked <- entries[nzchar(Sys.readlink(file.path(here, entries)))] folders <- entries[dir.exists(file.path(here, entries)) & !(entries %in% linked)] inner <- unlist(lapply(folders, function(folder) file.path(folder, list.dirs(file.path(here, folder), full.names = FALSE, recursive = FALSE))), use.names = FALSE) packaged <- Filter(function(folder) file.exists(file.path(here, folder, "DESCRIPTION")), c(folders, inner)) found <- c( if (length(linked) > 0L) paste0("a link, ", linked), intersect(c("library", ".Rprofile", ".Renviron"), entries), packaged, vapply(Filter(within, c(.libPaths(), packages_home())), shown, "", USE.NAMES = FALSE) ) unique(found) } sha <- function(path) unname(tools::sha256sum(path)) # tools::sha256sum came with R 4.5.0. has_sha256 <- function() "sha256sum" %in% getNamespaceExports("tools") script_dir <- function() { file <- sub("^--file=", "", grep("^--file=", commandArgs(FALSE), value = TRUE)) if (length(file) == 0L) return(getwd()) dirname(normalizePath(gsub("~+~", " ", file[1L], fixed = TRUE))) } plural <- function(n, one, many) if (n == 1L) one else many # One archive downloaded: its status, 0 where it came, and R's own words # where it did not, its first warning or its error. fetch <- function(source, archive) { why <- character(0) status <- tryCatch(withCallingHandlers( as.integer(utils::download.file(source, archive, mode = "wb", quiet = TRUE)), warning = function(w) { why <<- c(why, conditionMessage(w)) invokeRestart("muffleWarning") } ), error = function(e) { why <<- c(why, conditionMessage(e)) 1L }) list(status = status, why = why) } # Whether an R of its own, seeing the folder and R's own library alone, # loads every listed package from it: NULL where it does, or why not. unloadable <- function(library, names) { script <- tempfile(fileext = ".R") on.exit(unlink(script)) writeLines(c( "args <- commandArgs(TRUE)", ".libPaths(args[1L], include.site = FALSE)", "for (name in args[-1L]) loadNamespace(name, lib.loc = args[1L])" ), script) out <- suppressWarnings(system2(file.path(R.home("bin"), "Rscript"), c("--vanilla", shQuote(script), shQuote(library), names), stdout = TRUE, stderr = TRUE)) status <- attr(out, "status") if (is.null(status) || identical(as.integer(status), 0L)) NULL else "they could not be loaded by this R" } # Why the folder is not the one this file fills for packages.csv, or NULL # where it is: the mark it writes last is there and names this list, R and # platform, the folder holds exactly the listed packages, each at its # version and built by this R, and an R of its own loads them all. fault_of <- function(home, packages) { library <- file.path(home, "library") marker <- file.path(home, "complete") if (!file.exists(marker) || !identical(readLines(marker, warn = FALSE), stamp())) return("an earlier install into it did not finish") held <- sort(list.files(library)) if (!identical(held, sort(packages$package))) return("it does not hold exactly the packages packages.csv lists") for (i in seq_len(nrow(packages))) { fault <- package_fault(packages$package[i], packages$version[i], library, identical(packages$compiles_own_code[i], "TRUE")) if (!is.null(fault)) return(fault) } unloadable(library, packages$package) } # R CMD INSTALL sees this folder and R's own library alone, so a package # installed anywhere else never stands in for one packages.csv lists. install <- function(library, archives) { keep <- c("R_LIBS", "R_LIBS_USER", "R_LIBS_SITE") before <- Sys.getenv(keep, unset = NA, names = TRUE) on.exit({ set <- !is.na(before) if (any(set)) do.call(Sys.setenv, as.list(before[set])) if (any(!set)) Sys.unsetenv(keep[!set]) }) Sys.setenv(R_LIBS = library, R_LIBS_USER = library, R_LIBS_SITE = library) tools::Rcmd(c("INSTALL", "-l", shQuote(library), shQuote(archives))) } main <- function() { here <- script_dir() say <- function(...) cat(paste0(...), "\n", sep = "") say("Thesis-It analysis record, installing its packages in ", R.version.string, ".") if (!has_sha256()) stop("This record needs R ", R_MINIMUM, " or later. Install R ", R_VERSION, ".", call. = FALSE) beside <- planted(here) if (length(beside) > 0L) stop("This record's folder holds ", beside[1L], ", which no record carries. Unzip the record into an empty folder, put only the data files beside it, and run this again.", call. = FALSE) listed <- file.path(here, "packages.csv") if (!file.exists(listed)) stop("packages.csv is not beside this file. Unzip the whole record, then run this again.", call. = FALSE) if (!identical(sha(listed), PACKAGES_SHA256)) stop("packages.csv is not this record's: its SHA-256 differs.", call. = FALSE) packages <- utils::read.csv(listed, colClasses = "character", na.strings = character(0), check.names = FALSE, encoding = "UTF-8") if (!identical(names(packages), COLUMNS) || nrow(packages) == 0L) stop("packages.csv lists no packages this file can install.", call. = FALSE) home <- packages_home() library <- file.path(home, "library") count <- paste(nrow(packages), plural(nrow(packages), "package", "packages")) if (dir.exists(home)) { fault <- fault_of(home, packages) if (is.null(fault)) { say("Every package packages.csv lists is installed for this R in ", home, ". Now run Rscript --vanilla rerun.R with the data files.") return(invisible(NULL)) } say("The packages in ", home, " are not the ones packages.csv lists for this R: ", fault, ". They are removed and installed again.") unlink(home, recursive = TRUE) } if (file.exists(home)) stop(home, " is in the way of the folder this file keeps the packages in. Remove it, then run this again.", call. = FALSE) work <- tempfile("thesisit-packages-") dir.create(work) on.exit(unlink(work, recursive = TRUE), add = TRUE) archives <- file.path(work, paste0(packages$package, "_", packages$version, ".tar.gz")) for (i in seq_len(nrow(packages))) { one <- packages[i, ] got <- fetch(one$source, archives[i]) if (!identical(got$status, 0L) || !file.exists(archives[i])) stop(one$package, " ", one$version, " could not be downloaded from ", one$source, ". ", if (length(got$why) > 0L) paste0("R says: ", sub("[.]$", "", got$why[1L]), ". ") else "", "Nothing was installed.", call. = FALSE) if (!identical(sha(archives[i]), one$sha256)) stop(one$package, " ", one$version, " is not the archive packages.csv names: its SHA-256 differs. Nothing was installed.", call. = FALSE) } say("Every archive has the SHA-256 packages.csv names. Installing ", count, " for this R into ", home, ".") dir.create(library, recursive = TRUE) # Where anything after this fails, nothing is kept. failed <- function(why) { unlink(home, recursive = TRUE) stop(why, " Nothing was kept.", call. = FALSE) } if (!identical(as.integer(install(library, archives)), 0L)) failed("The packages could not be built: R's messages above say why.") for (i in seq_len(nrow(packages))) { fault <- package_fault(packages$package[i], packages$version[i], library, identical(packages$compiles_own_code[i], "TRUE")) if (!is.null(fault)) failed(paste0(fault, ".")) } loads <- unloadable(library, packages$package) if (!is.null(loads)) failed(paste0(loads, ".")) writeLines(stamp(), file.path(home, "complete")) say("Installed ", count, " for this R into ", home, ". To remove ", plural(nrow(packages), "it", "them"), ", delete that folder. Now run Rscript --vanilla rerun.R with the data files.") invisible(NULL) } environment() }) if (sys.nframe() == 0L) .thesisit_install$main()