Using Action Buttons
This article describes five patterns to use with Shiny’s action buttons and action links. Action buttons and action links are different from other Shiny widgets because they are intended to be used exclusively with observeEvent()
or eventReactive()
.
Pattern 1 - Command
Use observeEvent()
to trigger a command with an action button.
Example
In the code above, session$setCustomMessage()
generates a popup message. tags$head(tags$script(src = "message-handler.js"))
supplies the JavaScript that makes this possible. See this example to learn more aboutsendCustomMessage()
.
Why the pattern works
Action buttons do not automatically generate actions in Shiny. Like other widgets, action buttons maintain a state (a value). The state changes when a user clicks the button.
observeEvent()
observes a reactive value, which is set in the first argument of observeEvent()
. Whenever the value changes, observeEvent()
will run its second argument, which should be a block of code surrounded in braces.
This pattern uses observeEvent()
to connect the change in an action button’s value to the code that the action button should trigger.
Tips
observeEvent()
isolates the block of code in its second argument withisolate()
.observeEvent()
only notices changes in the value of the action button. It does not matter what the actual value of the button is. If your code depends on the value of the action button, it may be mis-written.
Pattern 2 - Delay reactions
Use eventReactive()
to delay reactions until a user clicks the action button.
Example
Why the pattern works
eventReactive()
creates a reactive expression that monitors a reactive value, which is set in the first argument of eventReactive()
. The expression will be invalidated whenever the value changes, but it will ignore changes in other reactive values.
Complete this pattern by using the reactive expression created by eventReactive()
in rendered output. Output that depends on the expression will not update until the expression is invalidated, i.e. until the action button is clicked.
Tips
Like
observeEvent()
,eventReactive()
isolates the block of code in its second argument withisolate()
.eventReactive()
returnsNULL
until the action button is clicked. As a result, the graph does not appear until the user asks for it by clicking “Go”.
Pattern 5 - Reset on tab change
Observe the value of a tabsetPanel()
, navlistPanel()
, or navbarPage()
with observeEvent()
to rest the value of an object each time your user switches tabs.
Example
Why this pattern works
This pattern extends the previous reset pattern. You use observeEvent()
to reset an element of a reactive values object. However, instead of observing the value of an action button, you observe the value of a tab function.
tabsetPanel()
, navlistPanel()
, and navbarPage()
each combine multiple tabs (created with tabPanel()
) into a single ui object. These functions maintain a reactive value that contains the title of the current tab. When your user navigates to a new tab, this value changes. observeEvent()
resets the reactive value to NULL
when it does.
As with the patterns above, this pattern requires you to store and manipulate a value created with reactiveValues()
.
Tips
- Although the example uses
tabsetPanel()
, you can acheive the same effect withnavlistPanel()
andnavbarPage()
.
Recap
Action buttons and action links are meant to be used with one of observeEvent()
or eventReactive()
. You can extend the effects of an action button with reactiveValues()
.
- Use
observeEvent()
to trigger a block of code with an action button. - Use
eventReactive()
to update derived/calculated output with an action button. - Use
reactiveValues()
to maintain an object for multiple action buttons to interact with.