I want to create a shiny app, that filters data table with two select inputs, that one of them is multiple and dependent on another one. First select input is category and the second one is brand. Brand is dependent on category input. I already made two dependent select inputs, but when I go to the filtering process of data table I get stuck. Because when I run app and filter data table with dependent multiple select, I get error " Warning in df$C_BRAND == input$brand :
longer object length is not a multiple of shorter object length".
I understand that whe I choose only one value on the second select its good, but when there is more, filtering fales.
runApp(list(
ui = basicPage(
sidebarPanel(
selectInput("kat", "Kategorija", choices = unique(df1$C_CTG), selected = unique(df1$C_CTG)[1]),
tags$hr(),
selectInput("brand", "Brandas", choices = df1$C_BRAND[df1$C_CTG == unique(df1$C_CTG)[1]],
multiple = TRUE)
),
mainPanel(
DT::dataTableOutput("table")
)
),
server = function(input, output, session) {
observe({
kat <- input$kat
updateSelectInput(session, "brand", choices = df1$C_BRAND[df1$C_CTG == kat])
})
df <- main2019
filterData1 <- reactive({
df[which(df$C_CTG == input$kat & df$C_BRAND == input$brand),]
})
output$table <- DT::renderDataTable({
DT::datatable(filterData1(),selection="multiple",rownames = F)
})
}
))
I know that I have to change this code line
df[which(df$C_CTG == input$kat & df$C_BRAND == input$brand),]
But I dont know what to put there, that when I choose more options in select input, filtering would work.
Here I made simple small sample of my datatable:
C_CTG <- sample(c(1:5),8,replace=TRUE)
brand <- sample(c(6:10),8,replace=TRUE)
store <- sample(c("shoes", "phones", "food", "drinks", "pets"),8, replace = TRUE)
input <- data.frame(C_CTG,brand,store)
Here is example of my generated data table
I expect for example, that in my first select I would choose value "2" in input$C_CTG
and then in second select input$brand
I would get to choose from "7" or "10" values and depending on what I will choose (7 or 10 or both) data table would show one row where input$C_CTG=2
and input$brand=10
or two rows where input$C_CTG=2
and input$brand=7
or it could show 3 rows when I chose input$C_CTG=2
and input$brand=c(7,10)
I hope that this example will let you understand what I want to make.