Combination of updateSelectizeInput()
and observeEvent()
should meet your requirement. Try this:
dataset <- data.frame("Letters" = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C"),
"Numbers" = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9),
"LettersNumbers" = c("G5", "G5", "G5", "G5", "G5", "G5", "F7", "F7", "F7", "F7", "F7", "F7", "E9", "E9", "E9", "E9", "E9", "E9"))
ui <- fluidPage(
br(),
fluidRow(
column(width = 4,
selectizeInput(inputId = "letters",
label = "Letters",
choices = unique(dataset$Letters),
multiple = TRUE),
br(),
selectizeInput(inputId = "numbers",
label = "Numbers",
choices = unique(dataset$Numbers),
multiple = TRUE),
br(),
selectizeInput(inputId = "lettersnumbers",
label = "Letters & Numbers",
choices = unique(dataset$LettersNumbers),
multiple = TRUE)
),
column(width = 4,
tableOutput(outputId = "table")
),
column(width = 4,
textOutput(outputId = "text")
)
)
)
server <- function(input, output, session) {
# Filter the initial dataset
dataset_filtered <- reactive({
dataset_filtered <- dataset
if (is.null(input$letters) & is.null(input$numbers) & is.null(input$lettersnumbers)) {
dataset_filtered <- dataset
updateSelectizeInput(session, inputId="letters",choices=dataset$Letters, selected=NULL)
updateSelectizeInput(session, inputId="numbers",choices=dataset$Numbers, selected=NULL)
updateSelectizeInput(session, inputId="lettersnumbers",choices=dataset$LettersNumbers, selected=NULL)
}else{
if (!is.null(input$letters)) {
dataset_filtered <- dataset_filtered %>%
filter(Letters %in% c(input$letters))
}
if (!is.null(input$numbers)) {
dataset_filtered <- dataset_filtered %>%
filter(Numbers %in% c(input$numbers))
}
if (!is.null(input$lettersnumbers)) {
dataset_filtered <- dataset_filtered %>%
filter(LettersNumbers %in% c(input$lettersnumbers))
}
}
return(dataset_filtered)
})
observeEvent(input$letters, {
updateSelectizeInput(session, inputId="lettersnumbers",choices=dataset_filtered()$LettersNumbers, selected=NULL)
updateSelectizeInput(session, inputId="numbers",choices=dataset_filtered()$Numbers, selected=NULL)
})
observeEvent(input$numbers, {
updateSelectizeInput(session, inputId="lettersnumbers",choices=dataset_filtered()$LettersNumbers, selected=NULL)
updateSelectizeInput(session, inputId="letters",choices=dataset_filtered()$Letters, selected=NULL)
})
observeEvent(input$lettersnumbers, {
updateSelectizeInput(session, inputId="letters",choices=dataset_filtered()$Letters, selected=NULL)
updateSelectizeInput(session, inputId="numbers",choices=dataset_filtered()$Numbers, selected=NULL)
})
# Display filtered table
output$table <- renderTable({
dataset_filtered()
})
# Display warning message
output$text <- renderText({
if (nrow(dataset_filtered()) == 0) {
print("No combinations available")
}
})
}
shinyApp(ui = ui, server = server)