daattali / shinyjs

đź’ˇ Easily improve the user experience of your Shiny apps in seconds
https://deanattali.com/shinyjs
Other
728 stars 119 forks source link

reset() function does not work with actionBttn() #267

Closed alessandroaccettulli closed 1 year ago

alessandroaccettulli commented 1 year ago

In the following code we have on the left an actionBttn, on the right a pickerInput (and for both we display in output their input value). If you change the value of the pickerInput, then you press the left actionBttn, the pickerInput value reset to its original value as expected.

However now, if you substitute in the code the pickerInput with another actionBttn with same id of pickerInput and repeat the above process, the value of the right actionBttn does not reset.

library(shiny)
library(shinyMobile)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(shinyjs)

ui = dashboardPage(
  header = dashboardHeader(title = NULL,
                           titleWidth = NULL,
                           disable = FALSE,
                           .list = NULL,
                           leftUi = NULL,
                           controlbarIcon = icon(name = "fas fa-star", class = NULL, lib = "font-awesome"),
                           fixed = FALSE),
  sidebar = dashboardSidebar(disable = FALSE,
                             width = NULL,
                             collapsed = FALSE,
                             minified = TRUE,
                             id = NULL),
  body = dashboardBody(shinyjs::useShinyjs(),
                       fluidRow(column(boxPad(actionBttn(inputId = "in1",
                                                         label = "Action Bttn",
                                                         icon = NULL,
                                                         style = "unite",
                                                         color = "default",
                                                         size = "md",
                                                         block = FALSE,
                                                         no_outline = TRUE),
                                              color = "blue", 
                                              style = NULL),
                                       width = 3, 
                                       offset = 0),
                                column(boxPad(textOutput(outputId = "out1"),
                                              color = "blue", 
                                              style = NULL),
                                       width = 3, 
                                       offset = 0),
                                column(boxPad(pickerInput(inputId = "in2",
                                                          label = "PickerInput",
                                                          choices = list("x1" = "a",
                                                                         "x2" = "b",
                                                                         "x3" = "c",
                                                                         "x4" = "d"),
                                                          selected = NULL,
                                                          multiple = FALSE,
                                                          options = list(),
                                                          choicesOpt = NULL,
                                                          width = NULL,
                                                          inline = FALSE),
                                              color = "blue", 
                                              style = NULL),
                                       width = 3, 
                                       offset = 0),
                                column(boxPad(textOutput(outputId = "out2"),
                                              color = "blue", 
                                              style = NULL),
                                       width = 3, 
                                       offset = 0))),
  controlbar = NULL,
  footer = NULL,
  title = NULL,
  skin = NULL,                
  freshTheme = NULL,
  preloader = NULL,
  md = FALSE,
  options = NULL,
  scrollToTop = FALSE
)

server = function(input, output, session){
  output$out1 = renderPrint({
    as.character(input$in1)
  })

  output$out2 = renderPrint({
    as.character(input$in2)
  })

  observeEvent(
    eventExpr = {
      list(input$in1)
    },
    handlerExpr = {
      reset(id = "in2", asis = FALSE)
    },
    ignoreInit = TRUE)
}

app = function(){
  runApp(shinyApp(ui, server),
         port = getOption("shiny.port"),
         launch.browser = list(getOption("shiny.launch.browser"), getOption("viewer"))[[1]],
         host = getOption("shiny.host", "127.0.0.1"),
         workerId = "",
         quiet = FALSE,
         display.mode = c("auto", "normal", "showcase"),
         test.mode = getOption("shiny.testmode", FALSE))
}
app()
daattali commented 1 year ago

It is not possible to reset the value of an action button because shiny doesn't offer a way to change the value of a button. All other inputs have a corresponding "update" function that allow us to change its value, but action buttons don't.

This is a known limitation that is documented in the reset function:

Note that actionButton is not supported, meaning that you cannot reset the value of a button back to 0.