I'm currently building a Shiny application in which users can filter data based on five dropdown menus. Those menus should behave like Excel's filter function, e.g.: Choose option in menu #1, filter the data for the other four menus depending on the input of the first menu. Choose option in menu #2, filter again all other inputs for the menus, but (and thats very important) keep the input of menu #1 selected and(!) filtered based on the new input.
In a first try, I coded this structure with five observeEvent() functions, one for each dropdown menu. Which worked, but produced loads of code, as the filter and update mechanics are quite big.
I'm currently revising my code and I thought that I could collapse the five observeEvent() functions into one observe() function, as the major part of the filter mechanism is completly equal for all five menus.
The problem that I am encountering now is that I would need a function which tells me which input field has changed. The flush event now checks all input fields in the observe() function. A test with is.null() can't be used, as an empty dropdown menu is also a "change" that needs to be checked (e.g. a user changed his mind and now doesn't want to filter with menu #1)
Example code where the error occurs and a function like "is.active" or "ic.clicked" would be really useful:
observe({
#reset values on each flush event
which_input <<- ""
bmodel_content <<- ""
country_content <<- ""
company_content <<- ""
#check which input has changed
#save the name and content for later update statements
if(!is.null(input$bmodel))
{
bmodel_content <<- input$bmodel
which_input <<- "bmodel"
}
else if(is.null(input$bmodel))
{
bmodel_content <<- c(unique(data$business.model))
which_input <<- "bmodel"
}
if(!is.null(input$country))
{
country_content <<- input$country
which_input <<- "country"
}
else if(is.null(input$country))
{
country_content <<- c(unique(data$country))
which_input <<- "country"
}
(... another 3 if-else statements for the other 3 menus ...)
###
#ERROR: "which_input" should be set with the name of the changed input field
# now it is always set to the last if-else-statement
###
(... filter data and updateSelectizeInput statements...)
Is there a way to check for the changed menu or do I need to go back to observeEvent?
Many thanks in advance!
which_input
at all. For example,filter()
indplyr
might be a good choice where you can just list all your conditions as parameters, without worrying which one of them is changing. – Xiongbing Jin