Skip to contents

with_config() runs code with a temporary modification to the package's setup state. local_config() modifies the state for the duration of the current scope (e.g., within a function).

These functions are useful for testing or temporarily changing how your package's config_pviem() function configures the environment.

Usage

with_config(new, code)

local_config(new = list(), .local_envir = parent.frame())

Arguments

new

[list] A named list of new setup values to apply temporarily. The names should correspond to the arguments of config_pviem().

code

[any] Code to execute in the temporary environment (for with_config()).

.local_envir

[environment]⁠ Environment used for scoping the change in local_config().

Value

[any] The results of the evaluation of the code argument.

See also

config_pviem() for the underlying setup function, get_pviem_config() to retrieve current mappings

Examples

# \donttest{
# Temporarily change setup for a block of code
get_pviem_config()$admin # Default mappings
#> [1] "dist_code"
with_config(list(admin = c("province", "state")), {
  get_pviem_config()$admin # Temporary mappings within this block
})
#> [1] "province" "state"   
get_pviem_config()$admin # Back to default mappings
#> [1] "dist_code"

# Change setup for the duration of a function
my_function <- function() {
  local_config(list(monthly = TRUE))
  get_pviem_config()$monthly # Monthly mapping within this function
}
my_function()
#> [1] TRUE
get_pviem_config()$monthly # Back to default mapping
#> [1] FALSE
# }