reactive.value
self, value=MISSING, *, read_only=False) reactive.value(
Create a reactive value.
Reactive values are the source of reactivity in Shiny. Changes to reactive values invalidate downstream reactive functions (calc, effect, and render
functions decorated with @output
). When these functions are invalidated, they get scheduled to re-execute.
Shiny input values are read-only reactive values. For example, input.x
is a reactive value object, and to get the current value, you can call input.x()
or input.x.get()
. When you do that inside of a reactive function, the function takes a dependency on the reactive value.
Parameters
value :
T
| MISSING_TYPE = MISSING-
An optional initial value.
read_only : bool = False
-
If
True
, then the reactive value cannot beset()
.
Returns
:
-
An instance of a reactive value.
Raises
: SilentException
-
If get is called before a value is provided/set.
Note
A reactive value may only be read from within a reactive function (e.g., calc, effect, shiny.express.render.text, etc.) and, when doing so, the function takes a reactive dependency on the value (i.e., when the value changes, the calling reactive function will re-execute).
See Also
Examples
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
## file: app.py
from shiny import reactive
from shiny.express import input, render, ui
val = reactive.value(0)
@reactive.effect
@reactive.event(input.minus)
def _():
newVal = val.get() - 1
val.set(newVal)
@reactive.effect
@reactive.event(input.plus)
def _():
newVal = val.get() + 1
val.set(newVal)
with ui.sidebar():
ui.input_action_button("minus", "-1")
ui.input_action_button("plus", "+1")
@render.text
def value():
return str(val.get())
Methods
Name | Description |
---|---|
freeze | Freeze the reactive value. |
get | Read the reactive value. |
is_set | Check if the reactive value is set. |
set | Set the reactive value to a new value. |
unset | Unset the reactive value. |
freeze
reactive.value.freeze()
Freeze the reactive value.
Freezing is equivalent to unsetting the value, but it does not invalidate dependents.
get
reactive.value.get()
Read the reactive value.
Returns
:
T
-
A value.
Raises
: SilentException
-
If the value is not set.
: RuntimeError
-
If called from outside a reactive function.
is_set
reactive.value.is_set()
Check if the reactive value is set.
Returns
: bool
-
True
if the value is set,False
otherwise.
set
set(value) reactive.value.
Set the reactive value to a new value.
Parameters
value :
T
-
A value.
Returns
: bool
-
True
if the value was set to a different value andFalse
otherwise.
Raises
: RuntimeError
-
If called on a read-only reactive value.
unset
reactive.value.unset()
Unset the reactive value.
Returns
: None
-
True
if the value was set prior to this unsetting.