From 7c8cd1b0275c6cb47dc91f32885bdc166054c247 Mon Sep 17 00:00:00 2001 From: MLopez-Ibanez <2620021+MLopez-Ibanez@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:08:11 +0000 Subject: [PATCH] More use of withr. Rename a few functions to replace . with _ --- R/configurations.R | 2 +- R/generation.R | 2 +- R/irace.R | 12 ++++++------ R/model.R | 2 +- R/utils.R | 15 ++++++++------- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/R/configurations.R b/R/configurations.R index 0c99be98..4acea5c6 100644 --- a/R/configurations.R +++ b/R/configurations.R @@ -7,7 +7,7 @@ configurations_alloc <- function(colnames, nrow, parameters) r = NA_real_, c = NA_character_, o = NA_character_, - irace.internal.error("Unknown type '", type, "'")) + irace_internal_error("Unknown type '", type, "'")) } column_type <- function(x, n, types) diff --git a/R/generation.R b/R/generation.R index 9c587a36..d303f720 100644 --- a/R/generation.R +++ b/R/generation.R @@ -122,7 +122,7 @@ generate_sobol <- function(parameters, n, repair = NULL) r = NA_real_, c = NA_character_, o = NA_character_, - irace.internal.error("Unknown type '", param[["type"]], "'")) + irace_internal_error("Unknown type '", param[["type"]], "'")) set(confs, j = p, value = na_value) } } diff --git a/R/irace.R b/R/irace.R index 13e9f9c3..77bee0fa 100644 --- a/R/irace.R +++ b/R/irace.R @@ -612,16 +612,16 @@ irace <- function(scenario) irace_common <- function(scenario, simple, output.width = 9999L) { if (!simple) { - op <- options(width = output.width) # Do not wrap the output. - on.exit(options(op), add = TRUE) + withr::local_options(width = output.width) # Do not wrap the output. } scenario <- checkScenario(scenario) debugLevel <- scenario$debugLevel - if (debugLevel >= 1) { - op.debug <- options(warning.length = 8170) - if (!base::interactive()) options(error = irace.dump.frames) - on.exit(options(op.debug), add = TRUE) + if (debugLevel >= 1L) { + op <- list(warning.length = 8170L) + if (!base::interactive()) + op <- c(op, list(error = irace_dump_frames)) + withr::local_options(op) printScenario (scenario) } diff --git a/R/model.R b/R/model.R index 48d7b97c..e1caf1a8 100644 --- a/R/model.R +++ b/R/model.R @@ -46,7 +46,7 @@ initialiseModel <- function (parameters, configurations) sd <- (length(domain) - 1L) * 0.5 values <- match(configurations[[currentParameter]], domain) } else { - irace.internal.error("Unknown parameter type '", type, "'") + irace_internal_error("Unknown parameter type '", type, "'") } # Assign current parameter value to model. param <- mapply(c, sd, values, SIMPLIFY=FALSE, USE.NAMES=FALSE) diff --git a/R/utils.R b/R/utils.R index 0765f592..dc36665b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -22,7 +22,7 @@ irace.error <- function(...) ## R> debugger(iracedump) ## ## See help(dump.frames) for more details. -irace.dump.frames <- function() +irace_dump_frames <- function() { execDir <- getOption(".irace.execdir") if (!is.null(execDir)) cwd <- setwd(execDir) @@ -34,15 +34,16 @@ irace.dump.frames <- function() } # Print an internal fatal error message that signals a bug in irace. -irace.internal.error <- function(...) +irace_internal_error <- function(...) { .irace.bug.report <- paste0(.irace_msg_prefix, "An unexpected condition occurred. ", "Please report this bug to the authors of the irace package ") - op <- options(warning.length = 8170) - if (!base::interactive()) options(error = irace.dump.frames) - on.exit(options(op)) + op <- list(warning.length = 8170L) + if (!base::interactive()) + op <- c(op, list(error = irace_dump_frames)) + withr::local_options(op) # 6 to not show anything below irace.assert() bt <- capture.output(traceback(5)) warnings() @@ -63,7 +64,7 @@ irace.assert <- function(exp, eval_after = NULL) msg_after <- eval.parent(capture.output(eval_after)) msg <- paste0(msg, "\n", paste0(msg_after, collapse="\n")) } - irace.internal.error(msg) + irace_internal_error(msg) invisible() } @@ -225,7 +226,7 @@ test.type.order.str <- function(test.type) t.none =, # Fall-throught t.holm =, # Fall-throught t.bonferroni = "mean value", - irace.internal.error ("test.type.order.str() Invalid value '", + irace_internal_error ("test.type.order.str() Invalid value '", test.type, "' of test.type"))