【问题标题】:RShiny having only one filter availableR Shiny 只有一个过滤器可用
【发布时间】:2020-03-07 09:30:19
【问题描述】:

我有一个显示简单情节的闪亮仪表板应用程序。我正在使用 2 个不同的过滤器过滤为绘图提供数据的数据框。

我想要的是,应用程序是否有可能拥有它:

  1. 我在过滤器 1 中选择了一个值
  2. 如果我现在在过滤器 2 上过滤一个值,那么过滤器 1 将重置为其原始值
  3. 基本上我一次只允许 1 个过滤器

谢谢!

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。

标签: r shiny shinydashboard


【解决方案1】:

您想使用updateSelectInput,然后使用observeEvent 来捕捉过滤器值的变化。

library(shiny)
library(shinydashboard) 

rm(list=ls())

#####/UI/####

header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
  selectInput(inputId = "filter1", label = "Filter 1", selected = "ALL",
              choices = c("ALL", "Bananas", "Apples", "Oranges")
  ),
  selectInput(inputId = "filter2", label = "Filter 2", selected = "ALL",
              choices = c("ALL", "Carrots", "Peas", "Celery")
  )
)

ui <- dashboardPage(header, sidebar, body)

#####/SERVER/####
server <- function(session, input, output) { 

  observeEvent(input$filter2,{
    if (input$filter2 != "ALL") {
      updateSelectInput(session, "filter1", selected = "ALL")
      }
  })

  observeEvent(input$filter1,{
    if (input$filter1 != "ALL") {
      updateSelectInput(session, "filter2", selected = "ALL")
    }
  })

  }

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 2018-10-11
    • 2019-09-14
    • 2021-08-04
    • 2014-01-01
    • 2020-08-14
    • 2019-05-04
    • 2019-03-22
    相关资源
    最近更新 更多