Holds 'shiny' reactive values and reactive expressions under keys that can be added, removed and reordered, with dependencies tracked per key. The 'shiny' package offers reactiveValues() for keyed stored values, but it can neither remove nor reorder keys, and it has no keyed collection of computed values.
Keyed collections of shiny reactives whose keys can be added, removed and reordered, with dependencies tracked per key.
Shiny’s reactive building blocks leave a gap:
| A single one | A keyed collection | In this package | |
|---|---|---|---|
| Stored | reactiveVal() |
reactiveValues() |
reactive_vals() |
| Computed | reactive() |
none | reactive_exprs() |
A reactiveValues() object can gain keys, but it can’t remove or
reorder them
(rstudio/shiny#2439),
and shiny has no keyed collection of computed values. This package fills
both gaps with collections whose keys can be added, removed and
reordered: reactive_vals() for stored values, reactive_exprs() for
computed ones, and reactives() for both kinds at once.
The package grows out of two workarounds in
blockr.core. One is
trim_rv(), which removes keys from a reactiveValues() object through
shiny’s internals. The other is the reactives class, which holds the
computed inputs of variadic blocks.
library(shiny)
library(reactives)
# Let reactives be read at the console, as inside an app
reactiveConsole(TRUE)
x <- reactives(a = reactiveVal(1), b = reactive(2 * 21))
# Reading with `$` or `[[` returns a slot's value, as on reactiveValues()
x$a
#> [1] 1
x[["b"]]
#> [1] 42
# A slot that doesn't exist reads as NULL, as on a list
is.null(x$c)
#> [1] TRUE
# Assigning a value writes it to the slot, adding the slot if there is none
x$a <- 10
x$c <- "new"
# With `[`, a slot's reactive is read, bound or removed
is.reactive(x["a"])
#> [1] TRUE
x["d"] <- reactive(x$a + 1)
x["b"] <- NULL
names(x)
#> [1] "a" "c" "d"
Inside an observer or a reactive(), reading a slot creates a
dependency on that slot alone, and reading its value with $ or [[
one on the value as well. The reader re-runs when that slot is added,
replaced or removed, or when the value it read changes, but not when
other slots change.
Under development. Planned work is tracked in the issues.
Install the released version from CRAN:
install.packages("reactives")
Or the development version from GitHub:
# install.packages("pak")
pak::pak("nbenn/reactives")