Skip to content

Commit 8120da9

Browse files
CLEANUP: Prepare to set R options from env vars at package load
1 parent 969bfb3 commit 8120da9

File tree

3 files changed

+118
-11
lines changed

3 files changed

+118
-11
lines changed

R/options.R

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,107 @@
117117
#' @keywords internal
118118
#' @name progressr.options
119119
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+
}

R/utils.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ mprintf <- function(..., appendLF = TRUE, debug = getOption("progressr.debug", F
5656
message(now(), sprintf(...), appendLF = appendLF)
5757
}
5858

59+
mdebugf <- mprintf
60+
5961
mprint <- function(..., appendLF = TRUE, debug = getOption("progressr.debug", FALSE)) {
6062
if (!debug) return()
6163
message(paste(now(), capture_output(print(...)), sep = "", collapse = "\n"), appendLF = appendLF)
@@ -67,6 +69,10 @@ mstr <- function(..., appendLF = TRUE, debug = getOption("progressr.debug", FALS
6769
message(paste(now(), capture_output(str(...)), sep = "", collapse = "\n"), appendLF = appendLF)
6870
}
6971

72+
comma <- function(x, sep = ", ") paste(x, collapse = sep)
73+
74+
commaq <- function(x, sep = ", ") paste(sQuote(x), collapse = sep)
75+
7076
stop_if_not <- function(..., calls = sys.calls()) {
7177
res <- list(...)
7278
n <- length(res)

R/zzz.R

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
.onLoad <- function(libname, pkgname) {
2+
debug <- isTRUE(as.logical(Sys.getenv("R_PROGRESSR_DEBUG", "FALSE")))
3+
if (debug) options(progressr.debug = TRUE)
4+
debug <- getOption("progressr.debug", debug)
5+
6+
## Set package options based on environment variables
7+
update_package_options(debug = debug)
8+
29
## R CMD check
310
if (in_r_cmd_check()) {
411
options(progressr.demo.delay = 0.0)
512
}
613

7-
value <- Sys.getenv("R_PROGRESSR_DEMO_DELAY", NA_character_)
8-
if (!is.na(value)) {
9-
value <- as.numeric(value)
10-
if (!is.na(value)) options(progressr.demo.delay = value)
11-
}
12-
1314
## R CMD build
1415
register_vignette_engine_during_build_only(pkgname)
1516

1617
## Register a global progression handler on load?
17-
global <- Sys.getenv("R_PROGRESSR_GLOBAL_HANDLER", "FALSE")
18-
global <- getOption("progressr.global.handler", as.logical(global))
19-
if (isTRUE(global)) {
18+
if (isTRUE(getOption("progressr.global.handler", FALSE))) {
2019
## UPDATE It is not possible to register a global calling handler when
2120
## there is already an active condition handler as it is here because
2221
## loadNamespace()/library() uses tryCatch() internally. If attempted,
@@ -25,5 +24,3 @@
2524
# register_global_progression_handler()
2625
}
2726
}
28-
29-

0 commit comments

Comments
 (0)