How to allow user to deselect selectizeInput items?

In the following bit of MWE code using the selectizeInput() function, the user selects which variable to filter the mtcars data by. When running the code you'll see where you can select the filtering variable in the user input box labeled "Variable".

However, how can this be changed so the user can deselect a variable after it has been entered into the "Variable" user input box, and thus update the table? By clicking on a little "x" next to the items appearing in the box would be ideal. For example, I have used selectizeGroupUI() before and those selected items can be easily deselected, and the related table updated, by clicking on a little "x" that appears next to each item.

library(shiny)

shinyApp(
  
  ui = fluidPage(
    uiOutput("filter"),
    tableOutput("data")
  ),
  
  server = function(input, output) {
    
    output$filter <- 
      renderUI({
        selectizeInput("variable", "Variable:",
                       c("Cylinders" = "cyl","Trans" = "am","Gears" = "gear"),
                       multiple = TRUE
                       )
      })
    
    output$data <- renderTable({
      req(input$variable)
      mtcars[, c("mpg", input$variable), drop = FALSE]
    }, rownames = TRUE)
  }
)

Upvotes: 1

Views: 216

Answers (1)

oskjerv
oskjerv

Reputation: 198

Is pickerInput from {shinyWidgets} an option?.

Doc: https://dreamrs.github.io/shinyWidgets/reference/pickerInput.html

pickerInput("variable",
            "Variable:",
            choices =  c("Cylinders" = "cyl","Trans" = "am","Gears" = "gear"),
            multiple = TRUE,
            selected = NULL,
            options = list(
                  title = "Select variables",
                  `actions-box` = TRUE,
                  `deselect-all-text` = "Remove"
                ))

Upvotes: 1

Related Questions