2017-12-01 1 views
0

Beispiel entnommen aus: Filter one selectInput based on selection from another selectInput?Dependent Filter in glänzenden Eingänge

Ich versuche, eine glänzende App zu erstellen, in dem der Benutzer mehrere Kisten und dann werden einige Daten erzeugt auswählen können. Ich verstehe nicht, ob ich zum Beispiel zuerst auf "Mars" klicke, dann wird die zweite Option Candy gefiltert, ich möchte nun "Snickers" auswählen, wieso stellt alles beim Klick auf Snickers wieder her?

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) 

Antwort

0

das Problem kommt aus, dass Sie Ihre UI machen codependent der Eingangsvariablen und wenn man die ganze UI ändert erneut gerendert einschließlich der Werte der Eingangsvariablen. Dieser Anwendungsfall ist besser mit den update*Input Funktionen. Hier ist eine funktionierende Version für Ihr Beispiel

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)