Ayub
Ayub

Reputation: 510

R Shiny Ignore placeholder label/value filtering with selectInput

UI

selectInput("marital_status", "Marital Status: ", choices = c("SELECT", setNames(marital_status$m_id,marital_status$m_name)))

Server

selected_members <- reactive(
    
      tmk %>% filter(between(AGE, input$age[1], input$age[2])) %>% 
        filter(between(ROOMS, input$room[1], input$room[2])) %>% 
        filter(between(PMT, input$pmt[1], input$pmt[2])) %>% 
        filter(MARITAL_STATUS %in% input$marital_status)
  )

Currently the last filter on MARITAL_STATUS results in 0 results in selected_memebers data frame as the selectInput returned value at input$marital_status initializes to "SELECT" when app is first run, is there any way the "placeholder"/"default" selection to be ignored from filter, I am not quite sure of exact format, any help would be greatly appreciated!

P.S: Code works fine with individual selections with MARITAL_STATUS input values/selections

Upvotes: 0

Views: 263

Answers (2)

Ayub
Ayub

Reputation: 510

Below is the correct syntax if you'd like "filter" to initally ignore the placeholder text and search only when user makes any valid selection (or back to placeholder text in that case it returns a larger dataset i.e. skipping the filter)

tmk %>% filter(between(AGE, input$age[1], input$age[2])) %>% 
        filter(between(ROOMS, input$room[1], input$room[2])) %>% 
        filter(between(PMT, input$pmt[1], input$pmt[2])) %>%
        {if(input$marital_status != "SELECT") filter(., MARITAL_STATUS == input$marital_status) else .}

Upvotes: 0

lz100
lz100

Reputation: 7330

simply add a req on the first line inside your reactive. Remeber to wrap your expression in reactive with {}

selected_members <- reactive({
    req(input$marital_status != "SELECT")
    tmk %>% filter ...
})

Upvotes: 0

Related Questions