I'm writing a Shiny module to filter my data depending on the users' input. Basically if the user chooses a country and a region, the program will choose the dataset corresponding to this country and filter the specific region. Once the specific region is filtered, it filters the sex.
# ui part
target_pop_UI <- function(id){
ns <- NS(id)
tagList(
selectInput(inputId = ns("input1"), label = "Country", choices = c("ITA", "BFA", "ZMB")),
selectInput(inputId = ns("input2"), label = "Region", choices = NULL),
selectInput(inputId = ns("input3"), label = "Sex", choices = NULL)
)
}
# server part
target_pop <- function(input, output, session){
observeEvent(input$input1,{
updateSelectInput(session = session, inputId = "input2", label = "Region", choices = c(get(input$input1)%>%
distinct(REGION), "All")
})
observeEvent(input$input2,{
updateSelectInput(session = session, inputId = "input3", label = "Sex", choices = c(get(input$input1)%>%
filter(if(input$input2 != "All") {REGION == input$input2})%>%
distinct(SEX_LABELS)
)}
}
This works if the user chooses a specific Region. But if he chooses "All", I want to not filter the dataset.
I've tried different options (in the second observeEvent) but it doesn't work :
filter(if(input$input2 != "All") {REGION == input$input2})
#or
filter(if(input$input2 != "All") {REGION == input$input2} else{})
#or
filter(if(input$input2 != "All") {REGION == input$input2} else{NULL})
The displayed error is :
Error in : Argument 2 filter condition does not evaluate to a logical vector
To sum up, what I want is to filter if Region != "All" then FILTER else DO NOTHING.
Thanks if someone could help me.
choices = NULL
but I'm assuming that's not the case : you can tryfilter(ifelse(input$input2 %in% [the choice statement in your input2],REGION == input$input2, TRUE))
. – ciceroupdateSelectInput(session = session, inputId = "input2", label = "Region", choices = c(get(input$input1)%>% distinct(REGION), "All")
– lucile