I'm trying to create an action button that has the following behavior once clicked:
- gets disabled
- changes label to temporary value while an arbitrary computation is ongoing
- gets re-enabled and reverts back to initial label once computation is over
Essentially, I want to create something very similar to this.
My problem is that I cannot get the button to temporarily change label during computation.
Here's the code I tried:
library(shiny)
library(shinyjs)
ui <- fluidPage(
shinyjs::useShinyjs(),
# create action button
fluidRow(
column(12, align = "center",
actionButton("button_analysis", label = "Label 1", icon = icon("rocket"))
)
)
)
server <- function(input, output, session) {
observe({
# disable button
shinyjs::disable("button_analysis")
# update button not working
updateActionButton(session, inputId = "button_analysis", label = "temporary label", icon = icon("sync", class = "fa-spin"))
# fake computation
Sys.sleep(2)
# enable button and change label after fake computation
on.exit({
shinyjs::enable("button_analysis")
updateActionButton(session, "button_analysis", label = "Label 2", icon = icon("rocket"))
})
}) %>%
bindEvent(input$button_analysis)
}
shinyApp(ui, server)
Could anyone explain to me why the updateActionButton() works while exiting the observeEvent, but not while inside it?
I just experienced the same problem. It is probably too late for your case but here is a working example:
Simply, there is no time to update the action button and thus a delay should be added, as it is suggested here too. The tricky part for me was where to place the calculations, and that is after we give some time to the button to be updated, hence within the delay chunk. I hope that answers your question.
I also think that the question "How to update action button label with updateActionButton in R shiny" does not fit to the description of the matter. This could be answered by the following chunk:
A title like "How to update an actionButton while calculating in R shiny" or "How to turn an actionButton into a loading button in R shiny" or something similar, could fit better to the issue.