local_inla_options.RdLocally sets INLA options with inla.setOption() and restores
when the calling function terminates, like withr::local_options() does for
global R options.
local_inla_options(..., .envir = parent.frame(), .save_only = FALSE)Named arguments to be passed to inla.setOption().
The environment in which the options should be set. Defaults to the calling environment.
If TRUE, the options are not set, but the current values
are saved and will be restored when the calling function terminates, even if
other code sets them directly by calling inla.setOption(). This can be
used to protect specific options against local changes that should not have
global effect. Default is FALSE, meaning that the options are set.
logical; If the option setting was successful, TRUE is returned,
otherwise FALSE.
print(paste0("Before: ", INLA::inla.getOption("num.threads")))
#> [1] "Before: 4:1"
local({
fun1 <- function() { INLA::inla.setOption("num.threads" = "1:1") }
fun2 <- function() {
local_inla_options(num.threads = NULL, .save_only = TRUE)
print(paste0("Local value: ", INLA::inla.getOption("num.threads")))
fun1()
print(paste0("Local changed value: ",
INLA::inla.getOption("num.threads")))
invisible()
}
fun2()
print(paste0("Restored: ", INLA::inla.getOption("num.threads")))
local_inla_options(num.threads = "2:1")
print(paste0("Change: ", INLA::inla.getOption("num.threads")))
fun2()
print(paste0("Restored: ",
INLA::inla.getOption("num.threads")))
})
#> [1] "Local value: 4:1"
#> [1] "Local changed value: 1:1"
#> [1] "Restored: 4:1"
#> [1] "Change: 2:1"
#> [1] "Local value: 2:1"
#> [1] "Local changed value: 1:1"
#> [1] "Restored: 2:1"
print(paste0("After: ", INLA::inla.getOption("num.threads")))
#> [1] "After: 4:1"