# Checks that R and each package are installed and new enough for this course.
# Stops with a list of everything that needs fixing, and how to fix it.
check_course_versions <- function(required, area) {
problems <- character(0)
to_install <- character(0)
found <- character(0)
for (name in names(required)) {
need <- required[[name]]
if (name == "R") {
have <- getRversion()
if (have < need) {
problems <- c(problems, sprintf(
"R is version %s; this course needs %s or newer. Install the current R from https://cran.r-project.org/ (see https://howisonlab.github.io/datawrangling/current/rtidyverse/rstudio_installation.html), then reinstall your packages.",
have, need))
}
found <- c(found, paste("R", have))
} else if (!nzchar(system.file(package = name))) {
problems <- c(problems, sprintf("%s is not installed.", name))
to_install <- c(to_install, name)
} else {
have <- packageVersion(name)
if (have < need) {
problems <- c(problems, sprintf(
"%s is version %s; this course needs %s or newer.", name, have, need))
to_install <- c(to_install, name)
} else if (isNamespaceLoaded(name) && getNamespaceVersion(name) != have) {
problems <- c(problems, sprintf(
"%s %s is installed, but this R session is still using %s. Restart R.",
name, have, getNamespaceVersion(name)))
}
found <- c(found, paste(name, have))
}
}
if (length(problems) > 0) {
fix <- "In RStudio choose Session > Restart R"
if (length(to_install) > 0) {
fix <- paste0(fix, ", then run this in the Console:\n install.packages(c(",
paste0('"', to_install, '"', collapse = ", "), "))\n")
}
stop("Course setup (", area, ") found problems:\n - ",
paste(problems, collapse = "\n - "), "\n\n",
fix, "\nThen run the setup chunk(s) again.", call. = FALSE)
}
cat("Course setup OK (", area, "): ", paste(found, collapse = ", "), "\n", sep = "")
}
check_course_versions(c(
R = "4.5.0", # CRAN builds Mac/Windows packages for the current and previous R only
tidyverse = "2.0.0",
dplyr = "1.2.0", # recode_values() and replace_values()
tidyr = "1.3.0", # separate_wider_delim()
gt = "1.0.0", # needed by gtExtras
gtExtras = "0.6.1" # works with ggplot2 4
), "R")
library(tidyverse)
library(gt)
library(gtExtras)