# Thesis-It analysis record: rerun.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 reruns the kept results this record lists from the data files named on # its command line, and checks every number each kept table holds against # R's answer at the position the record names, or against the request the # result kept. It reads only the files it is given and the record's own # members, writes rerun-report.txt beside itself, and sends nothing # anywhere. # # Rscript --vanilla rerun.R data.csv [more data files ...] # # --vanilla keeps R from reading startup files, such as an .Rprofile beside # this file, which could change what it does. Unzip the record into an empty # folder: it 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. It needs R 4.5.0 or later, for tools::sha256sum, # and was made with R 4.6.0. A result read from an SPSS file needs haven and # the packages packages.csv lists, at their versions: Rscript --vanilla # install-packages.R installs them into a folder of this computer's own, in # R's user data folder, and this file reads them from there alone. # # Each file is matched to its results by its SHA-256 and read as Thesis-It # read it on the student's device. A CSV file: UTF-8 when its bytes are # UTF-8, otherwise Windows-1252, without a byte-order mark and with "\n" # line endings, the separator the first line uses most outside quotes, and a # decimal comma only where a semicolon or a tab separates and most decimal # numbers in the first lines use one. An SPSS file (.sav or .zsav): with the # reader statistics-families.R defines, so the values SPSS declares missing # and its system-missing values are missing, a value with a label is its # code, a date is text and text is trimmed. Where a result declared values # that mean no answer, or prepared its items (reverse-scored some, or made a # scale's score), this is repeated next, as it was on the device. Each call # is checked against the grammar of the calls statistics-families.R takes # before R runs it. .thesisit_record <- local({ R_VERSION <- "4.6.0" R_MINIMUM <- "4.5.0" FAMILIES_SHA256 <- "3435914ad17cf025eb1151944c951234d79ebbf2c978fd475a0559733d995668" PACKAGES_SHA256 <- "f3a8915f219e5271ba4d6b7793d13aeeeefe2c11cf14f033fb56a3b8c6d6e65b" RECORD_PACKAGES <- c( "R6" = "2.6.1", "cli" = "3.6.6", "crayon" = "1.5.3", "glue" = "1.8.1", "rlang" = "1.3.0", "lifecycle" = "1.0.5", "magrittr" = "2.0.5", "pkgconfig" = "2.0.3", "utf8" = "1.2.6", "vctrs" = "0.7.3", "pillar" = "1.11.1", "tibble" = "3.3.1", "withr" = "3.0.3", "tidyselect" = "1.2.1", "forcats" = "1.0.1", "hms" = "1.1.4", "prettyunits" = "1.2.0", "progress" = "1.2.3", "cpp11" = "0.5.5", "bit" = "4.6.0", "bit64" = "4.8.6", "clipr" = "0.8.1", "tzdb" = "0.5.0", "vroom" = "1.7.1.9000", "readr" = "2.2.0", "haven" = "2.5.5" ) RECORD_COMPILED <- c("cli", "glue", "rlang", "magrittr", "utf8", "vctrs", "tibble", "bit", "bit64", "tzdb", "vroom", "readr", "haven") FAMILY_GROUPS <- c(omega = "psych", efa = "psych", cfa = "lavaan", plssem = "plspm") CALL <- "^\\.thesisit\\$(descriptives|frequencies|reliability|omega|efa|parallel|cfa|plssem|correlation|ttest|pairedttest|signedrank|mannwhitney|chisquare|regression|logistic|moderation|mediation|anova|kruskal|ancova|mixedanova)\\(d_[a-z0-9]{8,32}(, (c\\((\\d{1,3}(, \\d{1,3})*)?\\)|\\d{1,10}|0\\.\\d{1,6}|\"(pearson|spearman|gameshowell|tukey|none|wald|profile|minres|ml|paf|oblimin|promax|varimax|mlr|holm|bonferroni)\"))*\\)\\z" RESAMPLING <- c("mediation", "plssem", "parallel") NAMES <- c( descriptives = "Descriptive statistics", frequencies = "Frequencies", reliability = "Reliability (Cronbach's alpha)", omega = "Reliability (McDonald's omega)", efa = "Exploratory factor analysis", parallel = "Parallel analysis", cfa = "Confirmatory factor analysis", plssem = "PLS-SEM (partial least squares)", correlation = "Correlations", ttest = "Independent samples t-test", pairedttest = "Paired samples t-test", signedrank = "Wilcoxon signed-rank test", mannwhitney = "Mann-Whitney U test", kruskal = "Kruskal-Wallis test with Dunn's comparisons", chisquare = "Chi-square test of independence", regression = "Linear regression", logistic = "Binary logistic regression", moderation = "Moderation (interaction)", mediation = "Mediation", anova = "One-way ANOVA", ancova = "ANCOVA", mixedanova = "Mixed ANOVA" ) # WHATWG's windows-1252, bytes 0x80 to 0x9F. Every other byte is its own # code point. W1252 <- c(0x20ac, 0x81, 0x201a, 0x192, 0x201e, 0x2026, 0x2020, 0x2021, 0x2c6, 0x2030, 0x160, 0x2039, 0x152, 0x8d, 0x17d, 0x8f, 0x90, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x2dc, 0x2122, 0x161, 0x203a, 0x153, 0x9d, 0x17e, 0x178) # The packages the SPSS reader loads, at the versions this record was made # with, and the limits it reads a file within. SAV_PACKAGES <- c( "R6" = "2.6.1", "cli" = "3.6.6", "crayon" = "1.5.3", "glue" = "1.8.1", "rlang" = "1.3.0", "lifecycle" = "1.0.5", "magrittr" = "2.0.5", "pkgconfig" = "2.0.3", "utf8" = "1.2.6", "vctrs" = "0.7.3", "pillar" = "1.11.1", "tibble" = "3.3.1", "withr" = "3.0.3", "tidyselect" = "1.2.1", "forcats" = "1.0.1", "hms" = "1.1.4", "bit" = "4.6.0", "bit64" = "4.8.6", "clipr" = "0.8.1", "tzdb" = "0.5.0", "vroom" = "1.7.1.9000", "readr" = "2.2.0", "haven" = "2.5.5" ) # Those whose install compiles code of their own, so their build names # this computer's platform. SAV_COMPILED <- c("cli", "glue", "rlang", "magrittr", "utf8", "vctrs", "tibble", "bit", "bit64", "tzdb", "vroom", "readr", "haven") SAV_LIMITS <- c(rows = 200000, columns = 500, decoded = 167772160, labels = 20) sha <- function(path) unname(tools::sha256sum(path)) # tools::sha256sum came with R 4.5.0. has_sha256 <- function() "sha256sum" %in% getNamespaceExports("tools") # The text's bytes in UTF-8 when the file's bytes are Windows-1252, by # WHATWG's table: the five bytes it leaves undefined are C1 controls. windows_1252 <- local({ points <- c(0:127, W1252, 160:255) encoded <- lapply(points, function(p) if (p == 0) as.raw(0) else charToRaw(intToUtf8(p))) widths <- lengths(encoded) flat <- unlist(encoded) starts <- cumsum(c(1L, widths))[seq_along(widths)] function(bytes) { at <- as.integer(bytes) + 1L n <- widths[at] flat[rep(starts[at], n) + sequence(n) - 1L] } }) # Whether the bytes are UTF-8 as the browser's strict decoder reads it. # A NUL is UTF-8, and R's strings hold none, so each run between them is # checked on its own. is_utf8 <- function(bytes) { if (length(bytes) == 0L) return(TRUE) nul <- bytes == as.raw(0) if (!any(nul)) return(validUTF8(rawToChar(bytes))) parts <- split(bytes[!nul], cumsum(nul)[!nul]) all(vapply(parts, function(part) validUTF8(rawToChar(part)), logical(1))) } BOM <- as.raw(c(0xef, 0xbb, 0xbf)) QUOTE <- as.raw(0x22) LF <- as.raw(0x0a) CR <- as.raw(0x0d) has_bom <- function(bytes) length(bytes) >= 3L && identical(bytes[1:3], BOM) # Every line ending "\n": "\r\n" and a lone "\r" alike. line_feeds <- function(bytes) { cr <- which(bytes == CR) if (length(cr) == 0L) return(bytes) pair <- cr < length(bytes) & bytes[pmin(cr + 1L, length(bytes))] == LF bytes[cr[!pair]] <- LF if (any(pair)) bytes <- bytes[-cr[pair]] bytes } # The browser's decoder drops one byte-order mark, and the realm one more. decoded <- function(bytes) { if (is_utf8(bytes)) { if (has_bom(bytes)) bytes <- bytes[-(1:3)] if (has_bom(bytes)) bytes <- bytes[-(1:3)] } else bytes <- windows_1252(bytes) line_feeds(bytes) } # The bytes outside quotes, the quotes dropped. outside <- function(line) { q <- line == QUOTE line[!(q | cumsum(q) %% 2L == 1L)] } # The first lines, each ended by a "\n" outside quotes. first_lines <- function(bytes, count) { ends <- which(bytes == LF & cumsum(bytes == QUOTE) %% 2L == 0L) k <- min(length(ends), count) starts <- c(1L, ends + 1L) lines <- lapply(seq_len(k), function(i) bytes[seq_len(ends[i] - starts[i]) + starts[i] - 1L]) if (k < count && starts[k + 1L] <= length(bytes)) lines[[k + 1L]] <- bytes[starts[k + 1L]:length(bytes)] lines } fields_of <- function(bytes, sep) { cuts <- which(bytes == sep) starts <- c(1L, cuts + 1L) ends <- c(cuts - 1L, length(bytes)) lapply(seq_along(starts), function(i) if (ends[i] >= starts[i]) bytes[starts[i]:ends[i]] else raw(0)) } # What JavaScript's trim() takes from each end, as UTF-8 bytes: tabs, # line ends and spaces, the no-break space, Unicode's other spaces, the # line and paragraph separators, and the byte-order mark. Bytes, so no # locale or regular expression engine changes what is trimmed. SPACES <- lapply(c(9:13, 32, 160, 5760, 8192:8202, 8232, 8233, 8239, 8287, 12288, 65279), function(p) charToRaw(intToUtf8(p))) trimmed <- function(bytes) { opens <- function(s) length(bytes) >= length(s) && identical(bytes[seq_along(s)], s) closes <- function(s) length(bytes) >= length(s) && identical(bytes[length(bytes) - rev(seq_along(s)) + 1L], s) repeat { hit <- Find(opens, SPACES) if (is.null(hit)) break bytes <- bytes[-seq_along(hit)] } repeat { hit <- Find(closes, SPACES) if (is.null(hit)) break bytes <- bytes[seq_len(length(bytes) - length(hit))] } bytes } # Whether a trimmed field is a decimal number with this mark: an optional # sign, digits, the mark and at least one digit, in ASCII. decimal_with <- function(bytes, mark) { if (length(bytes) == 0L || any(bytes == as.raw(0))) return(FALSE) grepl(paste0("^[-+]?[0-9]*", mark, "[0-9]+$"), rawToChar(bytes), useBytes = TRUE) } # The separator and the decimal mark, by the realm's rule. csv_shape <- function(bytes) { lines <- first_lines(bytes, 200L) header <- if (length(lines) > 0L) outside(lines[[1L]]) else raw(0) tally <- function(byte) sum(header == as.raw(byte)) commas <- tally(0x2c) semicolons <- tally(0x3b) tabs <- tally(0x09) sep <- if (tabs > commas && tabs >= semicolons) "\t" else if (semicolons > commas) ";" else "," if (sep == ",") return(list(sep = sep, dec = ".")) comma <- 0L point <- 0L for (line in lines[-1L]) for (field in fields_of(outside(line), charToRaw(sep))) { value <- trimmed(field) if (decimal_with(value, ",")) comma <- comma + 1L else if (decimal_with(value, "[.]")) point <- point + 1L } list(sep = sep, dec = if (comma > point) "," else ".") } # The file read as the realm reads it: its text, then the same read.csv. read_like_realm <- function(path) { text <- decoded(readBin(path, "raw", n = file.size(path))) shape <- csv_shape(text) copy <- tempfile(fileext = ".csv") on.exit(unlink(copy)) writeBin(text, copy) utils::read.csv(copy, sep = shape$sep, dec = shape$dec, check.names = FALSE, na.strings = c("", "NA"), strip.white = TRUE, encoding = "UTF-8") } 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) } # Every package this record lists, from the folder install-packages.R # filled for this list, this R and its platform. An ambient package, # even at the same version, cannot supply an analysis or an import. packages_ready <- function(context, wanted) { home <- packages_home() library <- file.path(home, "library") again <- ". Run Rscript --vanilla install-packages.R, then run this again" listed <- file.path(script_dir(), "packages.csv") if (!file.exists(listed) || !identical(sha(listed), PACKAGES_SHA256)) stop("packages.csv is not this record's: its SHA-256 differs", call. = FALSE) if (!(wanted %in% names(RECORD_PACKAGES))) stop(context, ", and this record lists no ", wanted, " package", call. = FALSE) if (!file.exists(file.path(home, "complete")) || !identical(readLines(file.path(home, "complete"), warn = FALSE), stamp())) stop(context, ", and ", wanted, " ", RECORD_PACKAGES[[wanted]], " and the packages it needs are not installed for this R", again, call. = FALSE) if (!identical(sort(list.files(library)), sort(names(RECORD_PACKAGES)))) stop(context, ", and its library does not hold exactly the packages packages.csv lists", again, call. = FALSE) for (name in names(RECORD_PACKAGES)) { fault <- package_fault(name, RECORD_PACKAGES[[name]], library, name %in% RECORD_COMPILED) if (!is.null(fault)) stop(context, ", and ", fault, again, call. = FALSE) if (isNamespaceLoaded(name) && !identical(normalizePath(dirname(getNamespaceInfo(name, "path")), winslash = "/"), normalizePath(library, winslash = "/"))) stop(context, ", and ", name, " was loaded from another library before this file ran. Run Rscript --vanilla rerun.R in a new R", call. = FALSE) } .libPaths(library, include.site = FALSE) loaded <- tryCatch({ loadNamespace(wanted, lib.loc = library) TRUE }, error = function(e) FALSE) if (!loaded) stop(wanted, " could not be loaded on this computer. Run Rscript --vanilla install-packages.R again, which installs it afresh", call. = FALSE) } sav_ready <- function() packages_ready("it reads an SPSS file", "haven") # The actual call decides which package it needs, so changing a family # label in runs.csv cannot make R load an ambient package instead. family_ready <- function(call) { method <- sub("^[.]thesisit[$]([a-z]+).*", "\\1", call) group <- FAMILY_GROUPS[method] if (length(group) == 1L && !is.na(group)) packages_ready(paste0("it computes ", method, " with ", group), unname(group)) } # An SPSS file read as the realm reads it: by the reader # statistics-families.R defines, at the realm's limits. A file it cannot # read, one past the limits and one with no cases are refused, as the # realm refuses them. read_sav_like_realm <- function(path) { sav_ready() out <- .thesisit_sav(path, SAV_LIMITS[["rows"]], SAV_LIMITS[["columns"]], SAV_LIMITS[["decoded"]], SAV_LIMITS[["labels"]]) if (identical(out$refused, "too-large")) stop("its SPSS file holds more than Thesis-It reads", call. = FALSE) if (!is.null(out$refused)) stop("its SPSS file could not be read", call. = FALSE) if (nrow(out$frame) < 1L || ncol(out$frame) < 1L) stop("its SPSS file holds no cases", call. = FALSE) out$frame } # A run's file, read the way the realm read it, by runs.csv's reader. read_as <- function(path, reader) { if (identical(reader, "csv")) return(read_like_realm(path)) if (identical(reader, "sav")) return(read_sav_like_realm(path)) stop("its file is read in a way this record does not know", call. = FALSE) } # The codes a reading declared as no answer, "99:1,3;-9:2": each value # equal to a code is missing in the columns it names, by position. A # column of TRUE and FALSE holds no number, so a code removes nothing # there, though R compares TRUE as 1 and FALSE as 0. with_codes <- function(d, missing) { if (!nzchar(missing)) return(d) for (one in strsplit(missing, ";", fixed = TRUE)[[1L]]) { parts <- strsplit(one, ":", fixed = TRUE)[[1L]] code <- suppressWarnings(as.numeric(parts[1L])) columns <- suppressWarnings(as.integer(strsplit(parts[2L], ",", fixed = TRUE)[[1L]])) if (length(parts) != 2L || is.na(code) || length(columns) == 0L || anyNA(columns) || any(columns < 1L | columns > ncol(d))) stop("its codes for no answer name a column the file does not have", call. = FALSE) for (k in columns) { x <- d[[k]] if (!is.logical(x)) d[[k]][!is.na(x) & x %in% code] <- NA } } d } # The preparation a run's request made before its call, from runs.csv's # reversed and scales, after the codes for no answer. Each reversed item, # "5:1:5" (its column, then the lowest and the highest answer its scale # allows), becomes lowest + highest - value. Then each scale, "9:mean:1:3+7" # (its score's column, the mean or the sum, the items a row must answer, # then its items joined by "+", each a column of the file), is the mean or # the sum of its items where a row answers at least that many, and missing # where it answers fewer. The scores take the columns right after the # file's own. prepared <- function(d, reversed, scales) { if (!nzchar(reversed) && !nzchar(scales)) return(d) width <- ncol(d) refuse <- function(why) stop("its preparation", why, call. = FALSE) # Both fields are read as the record writes them and no other way, so a # form it never writes is refused and never guessed at. A column or a # count is a whole number with no sign, no zero before it and no # exponent. A range's end is a number as JavaScript prints one. Items are # joined by "+", entries are separated by ";", and nothing comes before, # between or after them. column <- "[1-9][0-9]*" number <- "-?(0|[1-9][0-9]*)([.][0-9]+)?(e[+-][0-9]+)?" written <- function(text, entry) grepl(paste0("^", entry, "(;", entry, ")*\\z"), text, perl = TRUE) if (nzchar(reversed) && !written(reversed, paste(column, number, number, sep = ":"))) refuse("'s reversed items are not ones this record takes") if (nzchar(scales) && !written(scales, paste0(column, ":(mean|sum|difference):", column, ":", column, "([+]", column, ")*"))) refuse("'s scores are not ones this record takes") entries <- function(text) if (nzchar(text)) strsplit(text, ";", fixed = TRUE)[[1L]] else character(0) whole <- function(text) { k <- suppressWarnings(as.numeric(text)) if (is.finite(k) && k == round(k)) suppressWarnings(as.integer(k)) else NA_integer_ } in_file <- function(k) !is.na(k) && k >= 1L && k <= width turned <- integer(0) for (one in entries(reversed)) { parts <- strsplit(one, ":", fixed = TRUE)[[1L]] k <- whole(parts[1L]) range <- suppressWarnings(as.numeric(parts[2:3])) if (!in_file(k) || k %in% turned || !all(is.finite(range)) || range[1L] >= range[2L]) refuse("'s reversed items are not ones this record takes") x <- suppressWarnings(as.numeric(d[[k]])) if (any(!is.na(x) & (x < range[1L] | x > range[2L]))) refuse(" finds an answer outside a reversed item's range") d[[k]] <- range[1L] + range[2L] - x turned <- c(turned, k) } scores <- list() for (one in entries(scales)) { parts <- strsplit(one, ":", fixed = TRUE)[[1L]] at <- whole(parts[1L]) needs <- whole(parts[3L]) items <- vapply(strsplit(parts[4L], "+", fixed = TRUE)[[1L]], whole, 0L, USE.NAMES = FALSE) count <- length(items) if (is.na(at) || at <= width || as.character(at) %in% names(scores) || count < 2L || count > 60L || anyDuplicated(items) > 0L || !all(vapply(items, in_file, TRUE)) || is.na(needs) || needs > count || (parts[2L] == "sum" && needs != count) || (parts[2L] == "mean" && 2L * needs < count) || (parts[2L] == "difference" && (count != 2L || needs != 2L))) refuse("'s scores are not ones this record takes") x <- matrix(vapply(items, function(k) suppressWarnings(as.numeric(d[[k]])), numeric(nrow(d))), nrow(d)) score <- if (parts[2L] == "sum") rowSums(x) else if (parts[2L] == "difference") x[, 1L] - x[, 2L] else rowMeans(x, na.rm = TRUE) score[rowSums(!is.na(x)) < needs] <- NA_real_ scores[[as.character(at)]] <- score } at <- sort(as.integer(names(scores))) if (!identical(at, width + seq_along(at))) refuse("'s scores do not take the columns right after the file's own") for (k in at) d[[k]] <- scores[[as.character(k)]] d } # R's answer to a run's call, first checked against the grammar of the # calls statistics-families.R takes. A family that resamples runs its # start, its steps and its finish, as the realm does. answer_of <- function(call, family, d) { if (!grepl(CALL, call, perl = TRUE) || !startsWith(call, paste0(".thesisit$", family, "("))) stop("its call is not one statistics-families.R takes", call. = FALSE) env <- new.env(parent = globalenv()) assign("d_thesisit", d, envir = env) run <- function(text) eval(str2lang(text), env) out <- if (family %in% RESAMPLING) { plan <- run(sub("(", "_start(", call, fixed = TRUE)) done <- 0 while (done < plan[1L]) { now <- .thesisit$resample(plan[2L]) if (!(now > done)) stop("its bootstrap drew no samples", call. = FALSE) done <- now } .thesisit$finish() } else run(call) out <- as.numeric(out) out[!is.finite(out)] <- NA_real_ out } STRICT <- 1e-12 AGREES <- "agrees" WITHIN <- "agrees within R's numerical tolerance" DIFFERS <- "differs" UNCHECKED <- "not rechecked" # One kept value against R's recomputed x: how far x falls outside the # kept value's rounding, relative to max(1, |x|). against_answer <- function(v, decimals, bound, x, tol) { if (is.na(x) || is.na(v)) return(list(outcome = DIFFERS, distance = NA_real_)) half <- if (is.na(decimals)) 0 else 0.5 * 10^(-decimals) gap <- if (bound == "<") x - v else if (bound == ">") v - x else abs(x - v) - half distance <- max(0, gap) / max(1, abs(x)) outcome <- if (distance <= STRICT) AGREES else if (distance <= tol) WITHIN else DIFFERS list(outcome = outcome, distance = distance) } # Each kept value of one run, checked. A value checked at a position # another kept value has names it (shared). checked <- function(values, answer, tol) { rows <- lapply(seq_len(nrow(values)), function(i) { line <- values[i, ] v <- suppressWarnings(as.numeric(line$value)) if (line$check == "answer") { at <- suppressWarnings(as.integer(line$position)) x <- if (!is.na(at) && at >= 1L && at <= length(answer)) answer[at] else NA_real_ out <- against_answer(v, suppressWarnings(as.integer(line$decimals)), line$bound, x, tol) return(c(line$position, if (is.na(x)) "NA" else sprintf("%.17g", x), out$outcome, if (is.na(out$distance)) "" else sprintf("%.3g", out$distance), line$shared)) } if (line$check == "request") { e <- suppressWarnings(as.numeric(line$expected)) same <- !is.na(e) && !is.na(v) && e == v return(c("", line$expected, if (same) AGREES else DIFFERS, "", "")) } c("", "", UNCHECKED, "", "") }) matrix(as.character(unlist(rows)), ncol = 5L, byrow = TRUE, dimnames = list(NULL, c("position", "recomputed", "outcome", "distance", "shared"))) } plural <- function(n, one, many) if (n == 1L) one else many summary_of <- function(head, outcomes, distances, shared) { checked <- outcomes != UNCHECKED agree <- outcomes %in% c(AGREES, WITHIN) differ <- outcomes == DIFFERS sharing <- checked & nzchar(shared) line <- paste0(head, sum(agree), " of ", sum(checked), " kept values agree.") if (any(sharing)) line <- paste0(line, " ", sum(sharing), " of the ", sum(checked), " ", plural(sum(sharing), "shares its", "share their"), " position in R's answer with another kept value.") if (any(outcomes == WITHIN)) line <- paste0(line, " ", sum(outcomes == WITHIN), " of them within R's numerical tolerance, the largest distance ", sprintf("%.3g", max(as.numeric(distances[outcomes == WITHIN]))), ".") if (any(differ)) { far <- suppressWarnings(max(as.numeric(distances[differ]), na.rm = TRUE)) line <- paste0(line, " ", sum(differ), " ", plural(sum(differ), "differs", "differ"), if (is.finite(far)) paste0(", the largest by ", sprintf("%.3g", far)) else "", ".") } if (any(!checked)) line <- paste0(line, " ", sum(!checked), " kept ", plural(sum(!checked), "number is", "numbers are"), " not rechecked: this record keeps no position in R's answer for ", plural(sum(!checked), "it", "them"), ".") line } # A column of runs.csv, empty where an earlier record has none. field <- function(run, name) if (name %in% names(run)) run[[name]] else "" # R's answer to each call on each file, computed once: the tables of one # result share their call. answers <- new.env() answer_for <- function(run, path) { family_ready(run$call) reversed <- field(run, "reversed") scales <- field(run, "scales") key <- paste(run$fingerprint, run$missing, reversed, scales, run$call, sep = "\n") if (!exists(key, envir = answers, inherits = FALSE)) assign(key, answer_of(run$call, run$family, prepared(with_codes(read_as(path, run$reader), run$missing), reversed, scales)), envir = answers) get(key, envir = answers, inherits = FALSE) } # One run rerun from its file: its summary and its values checked. rerun_one <- function(run, path, values, head) { answer <- answer_for(run, path) if (length(answer) != as.integer(run$length)) stop("R answered ", length(answer), " numbers where this record expects ", run$length, call. = FALSE) out <- checked(values, answer, as.numeric(run$tolerance)) table <- cbind(result = rep(run$run, nrow(values)), row = values$row, statistic = values$statistic, kept = paste0(values$bound, values$value), out) list(summary = summary_of(head, out[, "outcome"], out[, "distance"], out[, "shared"]), table = table, differs = any(out[, "outcome"] == DIFFERS)) } read_table <- function(path) utils::read.csv(path, colClasses = "character", na.strings = character(0), check.names = FALSE, encoding = "UTF-8") # The report, written to a file of its own beside it and renamed into # place: a rename replaces whatever has the report's name, a link # included, and never writes through it to a file elsewhere. write_report <- function(lines, path) { temporary <- tempfile("rerun-report-", tmpdir = dirname(path), fileext = ".txt") on.exit(if (file.exists(temporary)) unlink(temporary)) writeLines(enc2utf8(lines), temporary, useBytes = TRUE) if (!file.rename(temporary, path)) stop("rerun-report.txt could not be written beside this file.", call. = FALSE) invisible(path) } 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))) } main <- function(files) { here <- script_dir() said <- character(0) say <- function(...) { line <- paste0(...) cat(line, "\n", sep = "") said <<- c(said, line) } RNGkind("Mersenne-Twister", "Inversion", "Rejection") options(mc.cores = 1L) invisible(Sys.setlocale("LC_COLLATE", "C")) version <- paste(R.version$major, R.version$minor, sep = ".") say("Thesis-It analysis record, rerun in ", R.version.string, ".") if (!has_sha256()) stop("This record needs R ", R_MINIMUM, " or later. Install R ", R_VERSION, ".", call. = FALSE) if (version != R_VERSION) say("This record was made with R ", R_VERSION, ". This is R ", version, ", so the last places may differ.") 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 Rscript --vanilla rerun.R again.", call. = FALSE) if (length(files) == 0L) { say("Name the data files the results read: Rscript --vanilla rerun.R data.csv") quit(status = 2L) } families <- file.path(here, "statistics-families.R") if (!file.exists(families) || !identical(sha(families), FAMILIES_SHA256)) stop("statistics-families.R is not this record's: its SHA-256 differs.", call. = FALSE) sys.source(families, envir = globalenv()) runs <- read_table(file.path(here, "runs.csv")) kept <- read_table(file.path(here, "kept-values.csv")) given <- vapply(files, function(file) if (file.exists(file)) sha(file) else "", "", USE.NAMES = FALSE) failed <- FALSE for (i in seq_along(files)) { if (!nzchar(given[i])) say(basename(files[i]), ": No such file.") else if (!(given[i] %in% runs$fingerprint)) say(basename(files[i]), ": This is not a file the analysis read (SHA-256 differs).") else next failed <- TRUE } tables <- list() for (i in seq_len(nrow(runs))) { run <- runs[i, ] values <- kept[kept$run == run$run, , drop = FALSE] name <- if (is.na(NAMES[run$family])) run$family else NAMES[[run$family]] title <- if (nrow(values) > 0L) values$table[1L] else "" head <- paste0("Result ", run$run, ". ", name, " (", title, "): ") at <- match(run$fingerprint, given) if (is.na(at)) { say(head, "not rerun. Give the data file whose SHA-256 is ", run$fingerprint, ".") next } outcome <- tryCatch(rerun_one(run, files[at], values, head), error = function(e) e) if (inherits(outcome, "error")) { say(head, "could not be rerun: ", sub("[.]$", "", conditionMessage(outcome)), ".") failed <- TRUE next } say(outcome$summary) tables[[length(tables) + 1L]] <- outcome$table failed <- failed || outcome$differs } report <- c(said, "", "Each value kept and recomputed, the position in R's answer it was checked at counted from 1, and any other kept value checked there:") if (length(tables) > 0L) { all <- do.call(rbind, tables) report <- c(report, paste(colnames(all), collapse = "\t"), apply(all, 1L, paste, collapse = "\t")) } write_report(report, file.path(here, "rerun-report.txt")) if (failed) quit(status = 1L) invisible(NULL) } environment() }) if (sys.nframe() == 0L) .thesisit_record$main(commandArgs(trailingOnly = TRUE))