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)
No matching items

Relevant Functions

  • actionButton
    actionButton(inputId, label, icon = NULL, width = NULL, disabled = FALSE, ...)

  • bindEvent
    bindEvent(x, ..., ignoreNULL = TRUE, ignoreInit = FALSE, once = FALSE, label = NULL)

No matching items

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:

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

  2. Specify the inputId and label parameters of actionButton() 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:

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

  1. First, specify the code to execute on click (via a observe(), reactive(), or render*() object).

  2. Pipe the object from 1 into bindEvent(), and also supply the input value of the action button. This will prevent the object 1 from executing until the action button is clicked.

See also

  • Action Link

  • 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:

    1. Trigger Command
    2. Delay Reactions
    3. Dueling Buttons
    4. Reset Buttons
    5. Reset on Tab Change