0
votes

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.

1
It seems that you Region choices are choices = NULL but I'm assuming that's not the case : you can try filter(ifelse(input$input2 %in% [the choice statement in your input2],REGION == input$input2, TRUE)).cicero
These choises are updated in the server part : updateSelectInput(session = session, inputId = "input2", label = "Region", choices = c(get(input$input1)%>% distinct(REGION), "All")lucile

1 Answers

0
votes

I finally found a solution with assigning a reactive condition and then evaluate it in the filtering statement. Here's my code :

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)
  )
}

target_pop <- function(input, output, session){

  #update of region propositions depending on chosen country
  observeEvent(input$input1,{
    updateSelectInput(session = session, inputId = "input2", label = "Region", choices = c(get(input$input1)%>%
                        distinct(REGION), "All"))

  })


  cond1 <- reactive({if (input$input2 != "All") quote(REGION == input$input2) else TRUE})

  #Update of "sex propositions" depending on chosen region and country
  observeEvent(input$input2,{

    updateSelectInput(session = session, inputId = "input3", label = "Sex", choices = 
                        c(get(input$input1)%>%
                          filter(!!cond1())%>%
                          left_join(sex_labels)%>%
                          distinct(SEX_LABELS)%>%
                          rename(Sex = SEX_LABELS), "All"))
  })