Looks up key in defaults. If present and passes validator (when
supplied), returns the stored value; otherwise returns fallback.
Uses standard if/else instead of vectorized ifelse() to avoid
silent truncation of multi-valued defaults.
Arguments
- defaults
A named list of default values, or NULL. Individual entries may be a
reactive()/reactiveVal.- key
Character string — the name to look up.
- fallback
The value to return when
keyis absent or fails validation.- validator
An optional single-argument predicate function (e.g.,
is.numeric,is.logical). When supplied, the stored value is returned only ifvalidator(value)isTRUE. Reactive entries are validated on their resolved value.
Details
Entries that are a shiny::reactive() or shiny::reactiveVal() are resolved
with shiny::isolate() before validation, so this returns the reactive's
current value. See setup_reactive_defaults() for how modules keep such
entries live at render time.
Examples
get_default(list(color = "red"), "color", "black")
#> [1] "red"
get_default(list(), "missing", 10)
#> [1] 10
get_default(list(n = "x"), "n", 5, is.numeric)
#> [1] 5
get_default(list(color = shiny::reactiveVal("blue")), "color", "black")
#> [1] "blue"