R Shiny: Check if and which dropdown menu has been clicked within observe() -
i'm building shiny application in users can filter data based on 5 dropdown menus. menus should behave excel's filter function, e.g.: choose option in menu #1, filter data other 4 menus depending on input of first menu. choose option in menu #2, filter again other inputs menus, (and thats important) keep input of menu #1 selected and(!) filtered based on new input.
in first try, coded structure 5 observeevent() functions, 1 each dropdown menu. worked, produced loads of code, filter , update mechanics quite big.
i'm revising code , thought collapse 5 observeevent() functions 1 observe() function, major part of filter mechanism completly equal 5 menus.
the problem encountering need function tells me input field has changed. flush event checks input fields in observe() function. test is.null() can't used, empty dropdown menu "change" needs checked (e.g. user changed mind , doesn't want filter menu #1)
example code error occurs , function "is.active" or "ic.clicked" useful:
observe({ #reset values on each flush event which_input <<- "" bmodel_content <<- "" country_content <<- "" company_content <<- "" #check input has changed #save name , content 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" } (... 3 if-else statements other 3 menus ...) ### #error: "which_input" should set name of changed input field # set last if-else-statement ### (... filter data , updateselectizeinput statements...)
is there way check changed menu or need go observeevent?
many in advance!
this may not direct answer question, don't know filter function looks like. in following example, don't need know input changing, can still filter intended.
library(shiny) library(dplyr) ui <- shinyui(fluidpage( titlepanel("old faithful geyser data"), sidebarlayout( sidebarpanel( selectinput("cyl", "cyl", unique(mtcars$cyl), multiple = true), selectinput("vs", "vs", unique(mtcars$vs), multiple = true), selectinput("am", "am", unique(mtcars$am), multiple = true) ), mainpanel( tableoutput("table") ) ) )) server <- shinyserver(function(input, output) { output$table <- rendertable({ cyl_sel <- if (is.null(input$cyl)) unique(mtcars$cyl) else as.numeric(input$cyl) vs_sel <- if (is.null(input$vs)) unique(mtcars$vs) else as.numeric(input$vs) am_sel <- if (is.null(input$am)) unique(mtcars$am) else as.numeric(input$am) filter(mtcars, cyl %in% cyl_sel, vs %in% vs_sel, %in% am_sel) }) }) shinyapp(ui = ui, server = server)
Comments
Post a Comment