闪亮输入中的依赖滤波器

时间:2017-12-01 20:49:35

标签: r shiny shinydashboard

示例摘自:Filter one selectInput based on selection from another selectInput?

我正在尝试创建一个闪亮的应用程序,用户可以选择几个框,然后生成一些数据。我不明白我是否例如点击第一个" Mars",然后第二个选项Candy被过滤,我现在想选择" Snickers",为什么在点击士力架时一切都恢复了?

library(shiny)
library(shinydashboard)
library(shinyWidgets)
##
ui <- shinyUI({
  sidebarPanel(

    htmlOutput("brand_selector"),
    htmlOutput("candy_selector"))

})
##
server <- shinyServer(function(input, output) {
  candyData <- read.table(
    text = "Brand       Candy
    Nestle      100Grand
    Netle       Butterfinger
    Nestle      Crunch
    Hershey's   KitKat
    Hershey's   Reeses
    Hershey's   Mounds
    Mars        Snickers
    Mars        Twix
    Mars        M&Ms",
    header = TRUE,
    stringsAsFactors = FALSE)

  output$brand_selector <- renderUI({

    available2 <- candyData
    if(NROW(input$candy) > 0 ) available2 <- candyData[candyData$Candy %in% input$candy, ]

    pickerInput(
      inputId = "brand", 
      label = "Brand:",
      choices = as.character(unique(available2$Brand)),
      multiple = T,options = list(`actions-box` = TRUE))

  })

  output$candy_selector <- renderUI({

    available <- candyData
    if(NROW(input$brand > 0)) available <- candyData[candyData$Brand %in% input$brand, ]

    pickerInput(
      inputId = "candy", 
      label = "Candy:",
      choices = unique(available$Candy),
      multiple = T,options = list(`actions-box` = TRUE))

  })

})
##
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

问题来自于您的渲染UI与输入变量相关,并且当一个人更改整个重新渲染的UI时,包括输入变量的值。在这个用例中,您最好使用update*Input函数。这是您的示例

的工作版本
library(shiny)
library(shinydashboard)
library(shinyWidgets)
##
ui <- shinyUI({
  sidebarPanel(

    htmlOutput("brand_selector"),
    htmlOutput("candy_selector"))

})
##
server <- shinyServer(function(input, output,session) {
  candyData <- read.table(
    text = "Brand       Candy
    Nestle      100Grand
    Netle       Butterfinger
    Nestle      Crunch
    Hershey's   KitKat
    Hershey's   Reeses
    Hershey's   Mounds
    Mars        Snickers
    Mars        Twix
    Mars        M&Ms",
    header = TRUE,
    stringsAsFactors = FALSE)
  observeEvent({
    input$candy
  },
  {
    available2 <- candyData
    if(NROW(input$candy) > 0 ) available2 <- candyData[candyData$Candy %in% input$candy, ]
    updatePickerInput(
      session = session,
      inputId = "brand", 
      choices = as.character(unique(available2$Brand)),
      selected = input$brand
    )
  },
  ignoreInit = FALSE,
  ignoreNULL = FALSE)
  output$brand_selector <- renderUI({


    pickerInput(
      inputId = "brand", 
      label = "Brand:",
      choices = NULL,
      multiple = T,options = list(`actions-box` = TRUE))

  })
  observeEvent({
   input$brand
  },{
    available <- candyData
    if(NROW(input$brand > 0)) available <- candyData[candyData$Brand %in% input$brand, ]
    updatePickerInput(
      session = session,
      inputId = "candy",
      choices = unique(available$Candy),
      selected = input$candy
    )
  },
  ignoreInit = FALSE,
  ignoreNULL = FALSE)
  output$candy_selector <- renderUI({


    pickerInput(
      inputId = "candy", 
      label = "Candy:",
      choices = NULL,
      multiple = T,options = list(`actions-box` = TRUE))

  })

})
##
shinyApp(ui = ui, server = server)