Agenda
Reactive calculations
Your turn
Overriding reactivity
Side effects
@reactive.calc
creates calculations whose results can be used by one or more outputs @output
@render.table
def df():
rand = np.random.rand(input.n_rows(), 1)
df = pd.DataFrame(rand, columns=["col_1"])
return df
@output
@render.plot
def hist():
rand = np.random.rand(input.n_rows(), 1)
df = pd.DataFrame(rand, columns=["col_1"])
plot = (
ggplot(df, aes(x="col_1"))
+ geom_histogram(binwidth=0.1, fill="blue", color="black")
+ labs(x="Random Values", y="Frequency", title="Histogram of Random Data")
)
return plot
@reactive.calc
def sampled_df():
rand = np.random.rand(input.n_rows(), 1)
df = pd.DataFrame(rand, columns=["col_1"])
@output
@render.table
def df():
return sampled_df()
@output
@render.plot
def hist():
plot = (
ggplot(sampled_df(), aes(x="col_1"))
+ geom_histogram(binwidth=0.1, fill="blue", color="black")
+ labs(x="Random Values", y="Frequency", title="Histogram of Random Data")
)
return plot
@reactive.calc
decoratorAgenda
Reactive calculations
Your turn
Overriding reactivity
Side effects
Agenda
Reactive calculations
Your turn
Overriding reactivity
Side effects
reactive.event
to explicitly specify what should trigger recalculation for an output or calcfrom shiny.express import ui, render, input
from shiny import reactive
ui.input_text("input_txt", "Enter text")
ui.input_action_button("send", "Enter")
@render.text
@reactive.event(input.send)
def output_txt():
return input.input_txt()
@reactive.event
overrides the usual implicit dependency detection with an explicit trigger@reactive.calc
@reactive.event
is often used with action buttons or action linksAgenda
Reactive calculations
Your turn
Overriding reactivity
Side effects
@reactive.calc
@reactive.event
ui.show_modal
which triggers a modal window.@reactive.effect
decorator allows you to react to an input without returning a value@reactive.event
@output
+ @render.*
@reactive.calc
@reactive.effect