Action Button
#| standalone: true
#| components: [viewer]
#| viewerHeight: 200
library(shiny)
library(bslib)
ui <- page_fixed(
actionButton("update", "Show Time"),
textOutput("time")
)
server <- function(input, output) {
output$time <- renderText({
format(Sys.time(), "%a %b %d %X %Y")
}) |>
bindEvent(input$update)
}
shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
ui <- page_fixed(
actionButton("update", "Show Time"),
textOutput("time")
)
server <- function(input, output) {
output$time <- renderText({
format(Sys.time(), "%a %b %d %X %Y")
}) |>
bindEvent(input$update)
}
shinyApp(ui = ui, server = server)
Relevant Functions
-
actionButton
actionButton(inputId, label, icon = NULL, width = NULL, disabled = FALSE, ...)
-
bindEvent
bindEvent(x, ..., ignoreNULL = TRUE, ignoreInit = FALSE, once = FALSE, label = NULL)
Details
An action button appears as a button and has a value that increments each time the user presses the button. Combine an action button with bindEvent()
to specify what should happen when a user clicks the button.
Follow these steps to add an action button to your app:
Add
actionButton()
to the UI of your app to create an action button. Where you call this function will determine where the button will appear within the app’s layout.Specify the
inputId
andlabel
parameters ofactionButton()
to define the button’s identifier and label.
The value of an input component is accessible as a reactive value within the server()
function. To access the value of an action button:
- Use
input$<inputId>
to access the value of the action button, where<inputId>
is the identifier of the button, (e.g.,input$go
). The value is an integer that represents the number of times the action button has been clicked.
Action buttons are almost never used for their literal value, but to trigger a change that can be observed by bindEvent()
. To specify what should happen when the user clicks the action button:
First, specify the code to execute on click (via a
observe()
,reactive()
, orrender*()
object).Pipe the object from 1 into
bindEvent()
, and also supply theinput
value of the action button. This will prevent the object 1 from executing until the action button is clicked.
See also
Task Button to launch extended tasks that should run in the background
Submit Button to delay all reactions in the app until the button is clicked
Using Action Buttons, which demonstrates five patterns for using action buttons:
- Trigger Command
- Delay Reactions
- Dueling Buttons
- Reset Buttons
- Reset on Tab Change