local_inla_options {INLA} | R Documentation

## Helper for locally setting inla options

### Description

Locally sets INLA options with `inla.setOption()` and restores when the calling function terminates, like `withr::local_options()` does for global R options.

### Usage

    local_inla_options(..., .envir = parent.frame(), .save_only = FALSE)

### Arguments

`...` | Named arguments to be passed to `inla.setOption()`.
`.envir` | The environment in which the options should be set. Defaults to the calling environment.
`.save_only` | 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.

### Value

logical; If the option setting was successful, `TRUE` is returned, otherwise `FALSE`.

### Examples

    print(paste0("Before: ", INLA::inla.getOption("num.threads")))
    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")))
    })
    print(paste0("After: ", INLA::inla.getOption("num.threads")))

[Package INLA version 26.08.22 Index]