【问题标题】:Shiny selectizeInput with feature "remove all-at-once"具有“一次性删除全部”功能的闪亮 selectizeInput
【发布时间】:2017-06-29 20:48:41
【问题描述】:

示例:

以下闪亮示例app.R 文件包含selectizeInput UI。可以使用options = list(plugins= list('remove_button')) 删除选中的元素。

library(shiny)
library(dplyr)

ui= fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput(inputId= "cyl", label= "cyl", 
                     choices= sort(unique(mtcars$cyl)), 
                     selected= sort(unique(mtcars$cyl)),
                     multiple=T,
                     options = list(plugins= list('remove_button')))
    ),
    mainPanel(
      tableOutput("tab")
    )
  )
)

server= function(input, output) {
  df_filtered= reactive({
    mtcars %>%
    {if (is.null(input$cyl)) . else filter(., cyl %in% input$cyl)}
  })
  output$tab= renderTable(df_filtered())
}

shinyApp(ui, server)

问题:

是否有一个可以在闪亮中访问的 selectize.js 选项,它添加了一个功能“remove all-at-once”,而不是示例中所示的“remove one-by-one”?

我研究了selectize.js docu,但卡住了。

【问题讨论】:

  • 您希望它集成在表单中还是下面的按钮就足够了?
  • @BigDataScientist 最佳案例集成在表单中。
  • 如果没有人想出一个集成的方式,我可以用非花哨的方式提供帮助;)
  • 您可以按住删除按钮:)。说真的,我觉得你运气不好。让 BDS 为您编写一个按钮。看了下代码,remove_button这个功能比较新,目前还没有很多插件。如果有人有野心,他们当然可以写一个新的插件来做,但感觉代码会很乱。
  • @MikeWise 感谢您的调查。感谢您的判断。

标签: r shiny selectize.js


【解决方案1】:

我认为解决方案是使用reset_button,但是selected= 选项应该更改为最小值(一个选项?)因为它是重置值

library(shiny)
library(shinyjs)
library(dplyr)

ui= fluidPage(
  sidebarLayout(
    sidebarPanel(
      useShinyjs(),
      div(id = "form",
          selectizeInput(inputId = "cyl", 
                         label = "cyl",
                         choices = sort(unique(mtcars$cyl)), 
                         selected=sort(unique(mtcars$cyl))[1], multiple=TRUE)),
          actionButton("reset_input", "Reset")
    ),
    mainPanel(
      tableOutput("tab")
    )
  )
)

server= function(input, output) {

  observeEvent(input$reset_input, {
    shinyjs::reset("form")
  })

  df_filtered= reactive({
    mtcars %>%
    {if (is.null(input$cyl)) . else filter(., cyl %in% input$cyl)}
  })
  output$tab= renderTable(df_filtered())
}

shinyApp(ui, server)

按下Reset按钮后,立即清除所有selected值并返回selectizeInput的原始值。

【讨论】:

  • 非常感谢。这个解决方案在功能方面是可行的,不幸的是它添加了一个我需要避免的按钮。我一直在寻找一种集成在表单中的解决方案。感谢您的贡献和所花费的时间。
【解决方案2】:

我在寻找其他东西时偶然发现了这个答案,这是我最近必须解决的问题。这是我的解决方案,不需要额外的按钮。

library(shiny)
library(shinyjs)
library(dplyr)

ui= fluidPage(
  sidebarLayout(
    sidebarPanel(
          selectizeInput(inputId = "cyl", 
                         label = "cyl",
                         choices = c("All", sort(unique(mtcars$cyl))),
                         multiple = TRUE,
                         options = list(placeholder = "All"))
          ),
    mainPanel(
      tableOutput("tab")
    )
  )
)


server= function(input, output) {

  # This bit will revert the multi select back to the placeholder.
  # You might want to change the filtering logic further down stream though (depending on what actually want to display).
  observe({
    if("All" %in% input$cyl) {
      updateSelectizeInput(session = getDefaultReactiveDomain(),
                           "cyl",
                           choices = c("All", sort(unique(mtcars$cyl))),
                           options = list(placeholder = "All"))
    }
  })

  df_filtered= reactive({
    mtcars %>%
    {if (is.null(input$cyl)) . else filter(., cyl %in% input$cyl)}
  })
  output$tab= renderTable(df_filtered())
}

shinyApp(ui, server)

您也可以将plugins= list('remove_button') 添加到选项中,但需要将其添加到ui 部分和updateSelectizeInput() 函数中。

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 2019-01-24
    • 2018-12-22
    • 1970-01-01
    • 2017-04-04
    • 2019-06-18
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多