1.pick_cran <- function() {
2  # Return a CRAN repo URL, preferring RSPM binaries if available for this OS
3  rspm_template <- "https://packagemanager.rstudio.com/cran/__linux__/%s/latest"
4  supported_os <- c("xenial", "bionic", "centos7", "opensuse42", "opensuse15")
5
6  if (nzchar(Sys.which("lsb_release"))) {
7    os <- tolower(system("lsb_release -cs", intern = TRUE))
8    if (os %in% supported_os) {
9      return(sprintf(rspm_template, os))
10    }
11  }
12  if (file.exists("/etc/os-release")) {
13    os_release <- readLines("/etc/os-release")
14    vals <- sub("^.*=(.*)$", "\\1", os_release)
15    os <- intersect(vals, supported_os)
16    if (length(os)) {
17      # e.g. "bionic"
18      return(sprintf(rspm_template, os))
19    } else {
20      names(vals) <- sub("^(.*)=.*$", "\\1", os_release)
21      if (vals["ID"] == "opensuse") {
22        version <- sub('^"?([0-9]+).*"?.*$', "\\1", vals["VERSION_ID"])
23        os <- paste0("opensuse", version)
24        if (os %in% supported_os) {
25          return(sprintf(rspm_template, os))
26        }
27      }
28    }
29  }
30  if (file.exists("/etc/system-release")) {
31    # Something like "CentOS Linux release 7.7.1908 (Core)"
32    system_release <- tolower(utils::head(readLines("/etc/system-release"), 1))
33    # Extract from that the distro and the major version number
34    os <- sub("^([a-z]+) .* ([0-9]+).*$", "\\1\\2", system_release)
35    if (os %in% supported_os) {
36      return(sprintf(rspm_template, os))
37    }
38  }
39
40  return("https://cloud.r-project.org")
41}
42
43options(
44  Ncpus = parallel::detectCores(),
45  repos = tryCatch(.pick_cran(), error = function(e) "https://cloud.r-project.org"),
46  HTTPUserAgent = sprintf(
47    'R/%s R (%s)',
48    getRversion(),
49    paste(getRversion(), R.version$platform, R.version$arch, R.version$os)
50  )
51)
52