| Title: | Complex Reactive Values for 'shiny' |
|---|---|
| Description: | Create hybrid reactive objects that can be set imperatively but are validated reactively, ensuring their state is always consistent with their dependencies. |
| Authors: | Jon Harmon [aut, cre] (ORCID: <https://orcid.org/0000-0003-4781-4346>) |
| Maintainer: | Jon Harmon <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9017 |
| Built: | 2026-06-04 07:24:23 UTC |
| Source: | https://github.com/shinyworks/chains |
When used inside the validation_expr of a validated_reactive_val(), this
function acts as a pronoun to access the current value (before validation) of
that validated_reactive_val. This allows the validation expression to
reconcile the object's current state with its reactive dependencies.
.vrv(env = rlang::caller_env()).vrv(env = rlang::caller_env())
env |
( |
To reference .vrv() in a package and avoid R CMD check notes, you can
either import this function with #' @importFrom chains .vrv or call it
with the chains::.vrv() namespace.
The current value of the validated_reactive_val().
A convenience function to extract the error message attached to an object. A
method is implemented for validated_reactive_val() objects.
extract_error(x, ..., capture = TRUE)extract_error(x, ..., capture = TRUE)
x |
( |
... |
Additional arguments passed to methods. |
capture |
(length-1 |
If the object contains an error message, an object with class
captured-error if capture is TRUE or the error condition if capture
is FALSE. Depending on your purpose, you may need to signalCondition()
or rlang::cnd_signal() to actually signal the error. If the object does
not contain an error, NULL.
vrv <- validated_reactive_val( value = "good", default = "default", validation_expr = { if (.vrv() == "bad") { rlang::abort("is bad", class = "special_error") } .vrv() } ) shiny::isolate(extract_error(vrv)) vrv("bad") shiny::isolate(vrv()) captured_error <- shiny::isolate(extract_error(vrv)) class(captured_error) captured_error$message raw_error <- shiny::isolate(extract_error(vrv, capture = FALSE)) try(rlang::cnd_signal(raw_error))vrv <- validated_reactive_val( value = "good", default = "default", validation_expr = { if (.vrv() == "bad") { rlang::abort("is bad", class = "special_error") } .vrv() } ) shiny::isolate(extract_error(vrv)) vrv("bad") shiny::isolate(vrv()) captured_error <- shiny::isolate(extract_error(vrv)) class(captured_error) captured_error$message raw_error <- shiny::isolate(extract_error(vrv, capture = FALSE)) try(rlang::cnd_signal(raw_error))
A convenience function to check whether an object is currently using its
default value. A method is implemented for validated_reactive_val() objects.
is_default(x, ...)is_default(x, ...)
x |
( |
... |
Additional arguments passed to methods. |
A logical value indicating whether the object is currently using its
default value.
vrv <- validated_reactive_val( value = "good", default = "default", validation_expr = { if (.vrv() == "bad") { rlang::abort("is bad") } .vrv() } ) shiny::isolate(vrv()) shiny::isolate(is_default(vrv)) vrv("bad") shiny::isolate(vrv()) shiny::isolate(is_default(vrv))vrv <- validated_reactive_val( value = "good", default = "default", validation_expr = { if (.vrv() == "bad") { rlang::abort("is bad") } .vrv() } ) shiny::isolate(vrv()) shiny::isolate(is_default(vrv)) vrv("bad") shiny::isolate(vrv()) shiny::isolate(is_default(vrv))
Create a reactive value that can be updated imperatively (like a
shiny::reactiveVal()) but whose state is ultimately governed by a reactive
validation expression. This ensures that its value is always consistent with
its reactive dependencies before any downstream dependents are re-evaluated.
validated_reactive_val( validation_expr, value = NULL, default = NULL, label = NULL, env = rlang::caller_env() ) ## S3 method for class 'vrv' x$namevalidated_reactive_val( validation_expr, value = NULL, default = NULL, label = NULL, env = rlang::caller_env() ) ## S3 method for class 'vrv' x$name
validation_expr |
( |
value |
(various) The initial value. This value will be coerced via the
validation expression when accessed. If this value is reactive, an observer
with |
default |
(various, including |
label |
(length-1 |
env |
( |
x |
( |
name |
(length-1 |
A vrv object, which is a function with a custom class. Call it
with no arguments to (reactively) read the validated value. Call it with a
single argument to imperatively set the value; it will be automatically
validated on the next read. You can also access the most recent validation
error with my_vrv$error() and check if the current value is the default
with my_vrv$is_default().
library(shiny) ui <- fluidPage( selectInput("level", "Level", choices = c("A", "B")), uiOutput("group_ui"), textOutput("current_group") ) server <- function(input, output, session) { # `group_val` depends on `input$level` and can also be set by `input$group`. group_val <- validated_reactive_val( value = "A1", validation_expr = { # If the current value is not valid for the new level, reset it. valid_groups <- if (input$level == "A") { c("A1", "A2") } else { c("B1", "B2") } if (.vrv() %in% valid_groups) { .vrv() } else { valid_groups[[1]] } } ) # When the user changes the group input, imperatively update the vrv. observeEvent(input$group, { group_val(input$group) }) # The UI for the group dropdown is dynamic. output$group_ui <- renderUI({ choices <- if (input$level == "A") c("A1", "A2") else c("B1", "B2") selectInput("group", "Group", choices = choices, selected = group_val()) }) output$current_group <- renderText({ paste("Current Validated Group:", group_val()) }) } shinyApp(ui, server)library(shiny) ui <- fluidPage( selectInput("level", "Level", choices = c("A", "B")), uiOutput("group_ui"), textOutput("current_group") ) server <- function(input, output, session) { # `group_val` depends on `input$level` and can also be set by `input$group`. group_val <- validated_reactive_val( value = "A1", validation_expr = { # If the current value is not valid for the new level, reset it. valid_groups <- if (input$level == "A") { c("A1", "A2") } else { c("B1", "B2") } if (.vrv() %in% valid_groups) { .vrv() } else { valid_groups[[1]] } } ) # When the user changes the group input, imperatively update the vrv. observeEvent(input$group, { group_val(input$group) }) # The UI for the group dropdown is dynamic. output$group_ui <- renderUI({ choices <- if (input$level == "A") c("A1", "A2") else c("B1", "B2") selectInput("group", "Group", choices = choices, selected = group_val()) }) output$current_group <- renderText({ paste("Current Validated Group:", group_val()) }) } shinyApp(ui, server)
A wrapper around validated_reactive_val() that uses stbl::stabilize_chr()
to validate and coerce its value. This is a convenience function that
constructs the validation_expr for you.
vrv_chr( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, regex = NULL, label = NULL, env = rlang::caller_env() ) vrv_chr_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, regex = NULL, env = rlang::caller_env() )vrv_chr( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, regex = NULL, label = NULL, env = rlang::caller_env() ) vrv_chr_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, regex = NULL, env = rlang::caller_env() )
value |
(various) The initial value. This value will be coerced via the
validation expression when accessed. If this value is reactive, an observer
with |
default |
(various, including |
allow_null |
(length-1 |
allow_na |
(length-1 |
min_size |
(length-1 |
max_size |
(length-1 |
regex |
( |
label |
(length-1 |
env |
( |
allow_zero_length |
(length-1 |
A vrv object which returns a validated character vector.
A wrapper around validated_reactive_val() that uses stbl::stabilize_dbl()
to validate and coerce its value. This is a convenience function that
constructs the validation_expr for you.
vrv_dbl( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, label = NULL, env = rlang::caller_env() ) vrv_dbl_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, env = rlang::caller_env() )vrv_dbl( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, label = NULL, env = rlang::caller_env() ) vrv_dbl_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, env = rlang::caller_env() )
value |
(various) The initial value. This value will be coerced via the
validation expression when accessed. If this value is reactive, an observer
with |
default |
(various, including |
allow_null |
(length-1 |
allow_na |
(length-1 |
min_size |
(length-1 |
max_size |
(length-1 |
coerce_character |
(length-1 |
coerce_factor |
(length-1 |
min_value |
(length-1 |
max_value |
(length-1 |
label |
(length-1 |
env |
( |
allow_zero_length |
(length-1 |
A vrv object which returns a validated double vector.
A wrapper around validated_reactive_val() to help manage factor-like
values, especially in a cascading / dependent manner. This is a convenience
function that constructs the validation_expr for you by wrapping
stbl::stabilize_fct(). Note that the objects returned by the resulting
vrv are character vectors, not factors, to allow values to remain
unchanged when the allowed levels change but the value is still valid.
vrv_fct( levels, value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, to_na = character(), label = NULL, env = rlang::caller_env() ) vrv_fct_scalar( levels, value = NULL, default = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, to_na = character(), label = NULL, env = rlang::caller_env() )vrv_fct( levels, value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, to_na = character(), label = NULL, env = rlang::caller_env() ) vrv_fct_scalar( levels, value = NULL, default = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, to_na = character(), label = NULL, env = rlang::caller_env() )
levels |
( |
value |
(various) The initial value. This value will be coerced via the
validation expression when accessed. If this value is reactive, an observer
with |
default |
(various, including |
allow_null |
(length-1 |
allow_na |
(length-1 |
min_size |
(length-1 |
max_size |
(length-1 |
to_na |
( |
label |
(length-1 |
env |
( |
allow_zero_length |
( |
A vrv object which returns a factor-like character vector.
A wrapper around validated_reactive_val() that uses stbl::stabilize_int()
to validate and coerce its value. This is a convenience function that
constructs the validation_expr for you.
vrv_int( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, label = NULL, env = rlang::caller_env() ) vrv_int_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, env = rlang::caller_env() )vrv_int( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, label = NULL, env = rlang::caller_env() ) vrv_int_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, coerce_character = TRUE, coerce_factor = TRUE, min_value = NULL, max_value = NULL, env = rlang::caller_env() )
value |
(various) The initial value. This value will be coerced via the
validation expression when accessed. If this value is reactive, an observer
with |
default |
(various, including |
allow_null |
(length-1 |
allow_na |
(length-1 |
min_size |
(length-1 |
max_size |
(length-1 |
coerce_character |
(length-1 |
coerce_factor |
(length-1 |
min_value |
(length-1 |
max_value |
(length-1 |
label |
(length-1 |
env |
( |
allow_zero_length |
(length-1 |
A vrv object which returns a validated integer vector.
A wrapper around validated_reactive_val() that uses stbl::stabilize_lgl()
to validate and coerce its value. This is a convenience function that
constructs the validation_expr for you.
vrv_lgl( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, label = NULL, env = rlang::caller_env() ) vrv_lgl_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, env = rlang::caller_env() )vrv_lgl( value = NULL, default = NULL, allow_null = TRUE, allow_na = TRUE, min_size = NULL, max_size = NULL, label = NULL, env = rlang::caller_env() ) vrv_lgl_scalar( value = NULL, default = NULL, label = NULL, allow_null = FALSE, allow_zero_length = FALSE, allow_na = TRUE, env = rlang::caller_env() )
value |
(various) The initial value. This value will be coerced via the
validation expression when accessed. If this value is reactive, an observer
with |
default |
(various, including |
allow_null |
(length-1 |
allow_na |
(length-1 |
min_size |
(length-1 |
max_size |
(length-1 |
label |
(length-1 |
env |
( |
allow_zero_length |
(length-1 |
A vrv object which returns a validated logical vector.