|
| 1 | +# Install R dependencies, using only base R. |
| 2 | +# |
| 3 | +# Supported arguments: |
| 4 | +# |
| 5 | +# --all Install all the 'Depends', 'Imports', 'LinkingTo', and 'Suggests' dependencies |
| 6 | +# (automatically implies --build --test). |
| 7 | +# |
| 8 | +# --build Install the packages needed to build. |
| 9 | +# |
| 10 | +# --exclude=<pkg1,pkg2,...> Comma-delimited list of packages to NOT install. |
| 11 | +# |
| 12 | +# --include=<pkg1,pkg2,...> Comma-delimited list of additional packages to install. |
| 13 | +# These will always be installed, unless also used in "--exclude". |
| 14 | +# |
| 15 | +# --test Install packages needed to run tests. |
| 16 | +# |
| 17 | + |
| 18 | + |
| 19 | +# [description] Parse command line arguments into an R list. |
| 20 | +# Returns a list where keys are arguments and values |
| 21 | +# are either TRUE (for flags) or a vector of values passed via a |
| 22 | +# comma-delimited list. |
| 23 | +.parse_args <- function(args) { |
| 24 | + out <- list( |
| 25 | + "--all" = FALSE |
| 26 | + , "--build" = FALSE |
| 27 | + , "--exclude" = character(0L) |
| 28 | + , "--include" = character(0L) |
| 29 | + , "--test" = FALSE |
| 30 | + ) |
| 31 | + for (arg in args) { |
| 32 | + parsed_arg <- unlist(strsplit(arg, "=", fixed = TRUE)) |
| 33 | + arg_name <- parsed_arg[[1L]] |
| 34 | + if (!(arg_name %in% names(out))) { |
| 35 | + stop(sprintf("Unrecognized argument: '%s'", arg_name)) |
| 36 | + } |
| 37 | + if (length(parsed_arg) == 2L) { |
| 38 | + # lists, like "--include=roxygen2,testthat" |
| 39 | + values <- unlist(strsplit(parsed_arg[[2L]], ",", fixed = TRUE)) |
| 40 | + out[[arg_name]] <- values |
| 41 | + } else { |
| 42 | + # flags, like "--build" |
| 43 | + out[[arg]] <- TRUE |
| 44 | + } |
| 45 | + } |
| 46 | + return(out) |
| 47 | +} |
| 48 | + |
| 49 | +args <- .parse_args( |
| 50 | + commandArgs(trailingOnly = TRUE) |
| 51 | +) |
| 52 | + |
| 53 | +# which dependencies to install |
| 54 | +ALL_DEPS <- isTRUE(args[["--all"]]) |
| 55 | +BUILD_DEPS <- ALL_DEPS || isTRUE(args[["--build"]]) |
| 56 | +TEST_DEPS <- ALL_DEPS || isTRUE(args[["--test"]]) |
| 57 | + |
| 58 | +# force downloading of binary packages on macOS |
| 59 | +COMPILE_FROM_SOURCE <- "both" |
| 60 | +PACKAGE_TYPE <- getOption("pkgType") |
| 61 | + |
| 62 | +# CRAN has precompiled binaries for macOS and Windows... prefer those, |
| 63 | +# for faster installation. |
| 64 | +if (Sys.info()[["sysname"]] == "Darwin" || .Platform$OS.type == "windows") { |
| 65 | + COMPILE_FROM_SOURCE <- "never" |
| 66 | + PACKAGE_TYPE <- "binary" |
| 67 | +} |
| 68 | +options( |
| 69 | + install.packages.check.source = "no" |
| 70 | + , install.packages.compile.from.source = COMPILE_FROM_SOURCE |
| 71 | +) |
| 72 | + |
| 73 | +# always use the same CRAN mirror |
| 74 | +CRAN_MIRROR <- Sys.getenv("CRAN_MIRROR", unset = "https://cran.r-project.org") |
| 75 | + |
| 76 | +# we always want these |
| 77 | +deps_to_install <- c( |
| 78 | + "data.table" |
| 79 | + , "jsonlite" |
| 80 | + , "Matrix" |
| 81 | + , "R6" |
| 82 | +) |
| 83 | + |
| 84 | +if (isTRUE(BUILD_DEPS)) { |
| 85 | + deps_to_install <- c( |
| 86 | + deps_to_install |
| 87 | + , "knitr" |
| 88 | + , "markdown" |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +if (isTRUE(TEST_DEPS)) { |
| 93 | + deps_to_install <- c( |
| 94 | + deps_to_install |
| 95 | + , "RhpcBLASctl" |
| 96 | + , "testthat" |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +# add packages passed through '--include' |
| 101 | +deps_to_install <- unique(c( |
| 102 | + deps_to_install |
| 103 | + , args[["--include"]] |
| 104 | +)) |
| 105 | + |
| 106 | +# remove packages passed through '--exclude' |
| 107 | +deps_to_install <- setdiff( |
| 108 | + x = deps_to_install |
| 109 | + , args[["--exclude"]] |
| 110 | +) |
| 111 | + |
| 112 | +msg <- sprintf( |
| 113 | + "[install-r-deps] installing R packages: %s\n" |
| 114 | + , toString(sort(deps_to_install)) |
| 115 | +) |
| 116 | +cat(msg) |
| 117 | + |
| 118 | +install.packages( # nolint[undesirable_function] |
| 119 | + pkgs = deps_to_install |
| 120 | + , dependencies = c("Depends", "Imports", "LinkingTo") |
| 121 | + , lib = Sys.getenv("R_LIB_PATH", unset = .libPaths()[[1L]]) |
| 122 | + , repos = CRAN_MIRROR |
| 123 | + , type = PACKAGE_TYPE |
| 124 | + , Ncpus = parallel::detectCores() |
| 125 | +) |
0 commit comments