如何允许用户取消选择 selectizeInput 项目?

How to allow user to deselect selectizeInput items?

在使用 selectizeInput() 函数的 MWE 代码的以下位中,用户 select 设置了过滤 mtcars 数据所依据的变量。当 运行 代码时,您将在标记为“变量”的用户输入框中看到 select 过滤变量的位置。

但是,如何更改它以便用户可以在变量输入“变量”用户输入框后删除select 变量,从而更新table?通过单击框中出现的项目旁边的小“x”将是理想的。例如,我以前使用过 selectizeGroupUI(),那些 selected 的项目可以很容易地 deselected,相关的 table 更新,点击一个小“x”显示在每个项目旁边。

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)
  }
)

{shinyWidgets}中的pickerInput是一个选项吗?

文档: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"
                ))