shiny.reactive.Value#

class shiny.reactive.Value(value: shiny.types.MISSING_TYPE = MISSING, *, read_only: bool = 'False')#
class shiny.reactive.Value(value: shiny.reactive._reactives.T, *, read_only: bool = 'False')

Create a reactive value

Parameters
  • value (Union[~T, MISSING_TYPE]) – An optional initial value.

  • read_only (bool) – If True, then the reactive value cannot be set().

Return type

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.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

Inputs, Calc, Effect

Example

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
from shiny import *

app_ui = ui.page_fluid(
    ui.input_action_button("minus", "-1"),
    ui.input_action_button("plus", "+1"),
    ui.br(),
    ui.output_text("value"),
)

def server(input: Inputs, output: Outputs, session: Session):
    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)

    @output
    @render.text
    def value():
        return str(val.get())


app = App(app_ui, server)

Methods

freeze()

Freeze the reactive value.

get()

Read the reactive value.

is_set()

Check if the reactive value is set.

set(value)

Set the reactive value to a new value.

unset()

Unset the reactive value.