Search code examples
rshinyshinyjsshinywidgets

Reset Multiinput - choices don't clear


I have a Shiny app with a selectinput where users select a dataset. The variables of the dataset then appear in a shinywidgets multiInput as choices. I also have a reset button that is supposed to clear both input fields. However, it does not clear the non selected wrapper of the multi input (left side). I also tried to solve this without a shinyjs reset function and rather take another updateMultiInput approach but that didn't work either.

Here is a minimal reprex:

library(shiny)
library(shinyWidgets)
library(shinyjs)
library(purrr)

dfs <- keep(ls("package:datasets"), ~ is.data.frame(get(.x, "package:datasets")))


ui <- fluidPage(
  shinyjs::useShinyjs(),
  selectizeInput("dataset", "Select Dataset", choices = dfs, selected = NULL, multiple = TRUE, options = list(maxItems = 1)),
  multiInput("varselect", "Select Variables", choices = c("")),
  actionButton("reset_variables", "Reset Vars", style = "width: 145px; height: 40px")
)

server <- function(input, output, session) {

  data <- reactive({
    req(input$dataset)
    get(input$dataset, "package:datasets")
  })
  
  vars <- reactive(names(data()))
  
  observeEvent(input$dataset, {
    updateMultiInput(session, "varselect", choices = vars())

  })


  # reset inputs and lists if button is clicked
  observeEvent(input$reset_variables, {
    shinyjs::reset("dataset")
    shinyjs::reset("varselect")
  })
  
}

shinyApp(ui, server)


Solution

  • Perhaps shinyjs::reset() doesn't understand shinyWidgets::multiInput(). You can use updateMultiInput() instead:

    observeEvent(input$reset_variables, {
            shinyjs::reset("dataset")
            updateMultiInput(session, "varselect", choices = c(""))
        })