|
117 | 117 | #' @keywords internal |
118 | 118 | #' @name progressr.options |
119 | 119 | NULL |
| 120 | + |
| 121 | + |
| 122 | +get_package_option <- function(name, default = NULL, package = .packageName) { |
| 123 | + if (!is.null(package)) { |
| 124 | + name <- paste(package, name, sep = ".") |
| 125 | + } |
| 126 | + getOption(name, default = default) |
| 127 | +} |
| 128 | + |
| 129 | +# Set an R option from an environment variable |
| 130 | +update_package_option <- function(name, mode = "character", default = NULL, package = .packageName, split = NULL, trim = TRUE, disallow = c("NA"), force = FALSE, debug = FALSE) { |
| 131 | + if (!is.null(package)) { |
| 132 | + name <- paste(package, name, sep = ".") |
| 133 | + } |
| 134 | + |
| 135 | + mdebugf("Set package option %s", sQuote(name)) |
| 136 | + |
| 137 | + ## Already set? Nothing to do? |
| 138 | + value <- getOption(name, NULL) |
| 139 | + if (!force && !is.null(value)) { |
| 140 | + mdebugf("Already set: %s", sQuote(value)) |
| 141 | + return(getOption(name)) |
| 142 | + } |
| 143 | + |
| 144 | + ## name="Pkg.foo.Bar" => env="R_PKG_FOO_BAR" |
| 145 | + env <- gsub(".", "_", toupper(name), fixed = TRUE) |
| 146 | + env <- paste("R_", env, sep = "") |
| 147 | + |
| 148 | + env_value <- value <- Sys.getenv(env, unset = NA_character_) |
| 149 | + if (is.na(value)) { |
| 150 | + if (debug) mdebugf("Environment variable %s not set", sQuote(env)) |
| 151 | + |
| 152 | + ## Nothing more to do? |
| 153 | + if (is.null(default)) return(getOption(name)) |
| 154 | + |
| 155 | + if (debug) mdebugf("Use argument 'default': ", sQuote(default)) |
| 156 | + value <- default |
| 157 | + } |
| 158 | + |
| 159 | + if (debug) mdebugf("%s=%s", env, sQuote(value)) |
| 160 | + |
| 161 | + ## Trim? |
| 162 | + if (trim) value <- trim(value) |
| 163 | + |
| 164 | + ## Nothing to do? |
| 165 | + if (!nzchar(value)) return(getOption(name, default = default)) |
| 166 | + |
| 167 | + ## Split? |
| 168 | + if (!is.null(split)) { |
| 169 | + value <- strsplit(value, split = split, fixed = TRUE) |
| 170 | + value <- unlist(value, use.names = FALSE) |
| 171 | + if (trim) value <- trim(value) |
| 172 | + } |
| 173 | + |
| 174 | + ## Coerce? |
| 175 | + mode0 <- storage.mode(value) |
| 176 | + if (mode0 != mode) { |
| 177 | + suppressWarnings({ |
| 178 | + storage.mode(value) <- mode |
| 179 | + }) |
| 180 | + if (debug) { |
| 181 | + mdebugf("Coercing from %s to %s: %s", mode0, mode, commaq(value)) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if (length(disallow) > 0) { |
| 186 | + if ("NA" %in% disallow) { |
| 187 | + if (any(is.na(value))) { |
| 188 | + stop(sprintf("Coercing environment variable %s=%s to %s would result in missing values for option %s: %s", sQuote(env), sQuote(env_value), sQuote(mode), sQuote(name), commaq(value))) |
| 189 | + } |
| 190 | + } |
| 191 | + if (is.numeric(value)) { |
| 192 | + if ("non-positive" %in% disallow) { |
| 193 | + if (any(value <= 0, na.rm = TRUE)) { |
| 194 | + stop(sprintf("Environment variable %s=%s specifies a non-positive value for option %s: %s", sQuote(env), sQuote(env_value), sQuote(name), commaq(value))) |
| 195 | + } |
| 196 | + } |
| 197 | + if ("negative" %in% disallow) { |
| 198 | + if (any(value < 0, na.rm = TRUE)) { |
| 199 | + stop(sprintf("Environment variable %s=%s specifies a negative value for option %s: %s", sQuote(env), sQuote(env_value), sQuote(name), commaq(value))) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if (debug) { |
| 206 | + mdebugf("=> options(%s = %s) [n=%d, mode=%s]", |
| 207 | + dQuote(name), commaq(value), |
| 208 | + length(value), storage.mode(value)) |
| 209 | + } |
| 210 | + |
| 211 | + do.call(options, args = structure(list(value), names = name)) |
| 212 | + |
| 213 | + getOption(name, default = default) |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | +## Set package options based on environment variables |
| 218 | +update_package_options <- function(debug = FALSE) { |
| 219 | + update_package_option("demo.delay", mode = "numeric", debug = debug) |
| 220 | + |
| 221 | + ## However, not used |
| 222 | + update_package_option("global.handler", mode = "logical", debug = debug) |
| 223 | +} |
0 commit comments