reactive.event(*args, ignore_none=True, ignore_init=False)

Mark a function to react only when an "event" occurs.

Shiny's reactive programming framework is primarily designed for calculated values (calc) and side-effect-causing actions (effect) that respond to any of their inputs changing. That's often what is desired in Shiny apps, but not always: sometimes you want to wait for a specific action to be taken from the user, like clicking an input_action_button, before calculating or taking an action. You do not want the calculation or action to be prematurely triggered if other reactive values that it calls are invalidated. The reactive value (or function) which triggers other calculations or actions in this way is called an event.

These situations demand a more imperative, "event handling" style of programming, which @reactive.event() provides. It does this by using the isolate primitive under-the-hood to essentially "limit" the set of reactive dependencies to those in args. In other words, the event can call as many reactive values as it likes in its code body without taking a reactive dependency on them; it will be invalidated only when a dependency listed in args is invalidated.


*args: Callable[[], object] | Callable[[], Awaitable[object]] = ()

One or more callables that represent the event; most likely this will be a reactive input value linked to a input_action_button or similar (e.g.,, but it can also be a (reactive or non-reactive) function that returns a value.

ignore_none: bool = True

Whether to ignore the event if the value is None or 0.

ignore_init: bool = False

If False, the event triggers on the first run.


Type Description
Callable[[Callable[[], T]], Callable[[], T]] A decorator that marks a function as an event handler.


This decorator must be applied before the relevant reactivity decorator (i.e., @reactive.event must be applied before @reactive.effect, @reactive.calc, @render.ui, etc).


#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400

## file:
import random

from shiny import reactive
from import input, render, ui
from shiny.ui import output_ui

    This example demonstrates how `@reactive.event()` can be used to restrict
    execution of: (1) a `@render` function, (2) `@reactive.calc`, or (3)

    In all three cases, the output is dependent on a random value that gets updated
    every 0.5 seconds (currently, it is {output_ui("number", inline=True)}), but
    the output is only updated when the button is clicked.

# Always update this output when the number is updated
with ui.hold():

    def number():
        return val.get()

ui.input_action_button("btn_out", "(1) Update number")

# Since ignore_none=False, the function executes before clicking the button.
# (input.btn_out() is 0 on page load, but @@reactive.event() treats 0 as None for
# action buttons.)
@reactive.event(input.btn_out, ignore_none=False)
def out_out():
    return str(val.get())

ui.input_action_button("btn_calc", "(2) Show 1 / number")

def out_calc():
    return str(calc())

ui.input_action_button("btn_effect", "(3) Log number")

# Update a random number every second
val = reactive.value(random.randint(0, 1000))

def _():
    val.set(random.randint(0, 1000))

def calc():
    return 1 / val.get()

def _():
        ui.p("Random number! ", val.get()),