闪亮输入中的相关过滤器

Dependent filter in shiny inputs

示例取自:

我正在尝试创建一个闪亮的应用程序,用户可以在其中 select 多个框,然后生成一些数据。我不明白如果我先点击 "Mars",然后第二个选项 Candy 被过滤,我现在想 select "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)

问题来自于您的渲染 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)