通过 updatePickerInput shiny R 取消选择全部

Deselected All by updatePickerInput shiny R

我的 shiny 应用程序几乎没有 pickerInput 元素。默认情况下未选择任何内容。

pickerInput(
  inputId = "pickerInput1", 
  label = NULL, 
  choices = c("Yes", "No"), 
  options = list(
    `actions-box` = TRUE, 
    size = 12,
    `selected-text-format` = "count > 3"
  ), 
  multiple = TRUE
)

问题是我不知道如何在单击特殊按钮后清除所有这些(转到默认值)。不幸的是我可能不知道如何使用 updatePickerInput。我试过了:

  observeEvent(input$Clear_FilterButton,     {
    updatePickerInput(session, "pickerInput1", selected = NULL)
  })

但它不起作用 :( 知道我做错了什么吗?

如果您使用 shinyWidgets 中的 pickerInput,将 actions-box 设置为 TRUE 应该构建 Select All & 默认取消选择所有按钮。你不需要 updatePickerInput。单击您的 pickerInput 以查看这些按钮。

请参阅文档以获取更多详细信息:
https://github.com/dreamRs/shinyWidgets

更新跟进您的评论:

你的评论使问题更清楚了。您可以简单地使用 selected = "" 而不是 selected = NULL。这是一个工作示例:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  pickerInput(
    inputId = "pickerInput1", 
    label = NULL, 
    choices = c("Yes", "No"), 
    options = list(
      `actions-box` = TRUE, 
      size = 12
    ), 
    multiple = TRUE
  ),

  actionButton(
    inputId = "Clear_FilterButton",
    label = "Clear"
  )
)

server <- function(session, input, output) {
  observeEvent(input$Clear_FilterButton, {
    updatePickerInput(
      session, 
      "pickerInput1", 
      selected = ""
    )
  })
}

shinyApp(ui = ui, server = server)