Filtering the data when input is null : dplyr and shiny

Hi
I am looking to resolve an issue i have for shiny and dplyr in server.R. Please find the code for ui.R

dateInput("Date_1",label = "Enter Date",max = Sys.Date(),format = "yyyy-mm",value = "2016-8-01"),  
selectInput("Product_1",label = "Product",choices = unique(part_details_2$Product),multiple =TRUE,selected = ""),
selectInput("VSO_1",label = "VSO",choices = unique(failure_2$VS),multiple = TRUE,selected = " ")

Below is activity in server.R

temp_MIS03 = failure_2 %>% filter(MIS <= 03 & MFD >= input$Date_1 & Product == input$Product_1 & VS ==input$VSO_1) %>% group_by(MFD) %>% summarise(MIS03 = n()*1000)
  %>% left_join(disp_all,.,by = "MFD") %>% transmute(MFD ,MIS03 = MIS03/Quantity)

The user can choose either following criteria,
Product
VSO
VSO + Product
My code is only working only when both are selected.

To complicate :slight_smile: i need to have both "AND" or "OR" criteria in my code

So you issue is that when only one is selected the other input has a value of NULL. This is resulting in an error because

  1. I assume that you probably are not actually looking for nulls
  2. You can not use the == operator for NULL values. As a test see what you get when you put NULL == NULL in your console. You need to use is.null() to test for whether a value is or isn't a NULL.

I would recommend that you put some logic in your server code that handles the different cases. For example, you could do this:

if (is.null(input$Product_1)){
  temp_MIS03 = failure_2 %>% 
    filter(MIS <= 03 & MFD >= input$Date_1, VS ==input$VSO_1) %>% 
    group_by(MFD) %>% 
    summarise(MIS03 = n()*1000) %>% 
    left_join(disp_all,.,by = "MFD") %>% 
    transmute(MFD ,MIS03 = MIS03/Quantity)
} else if (is.null(input$VSO_1)) {
  temp_MIS03 = failure_2 %>% 
    filter(MIS <= 03 & MFD >= input$Date_1, Product == input$Product_1) %>% 
    group_by(MFD) %>% 
    summarise(MIS03 = n()*1000) %>% 
    left_join(disp_all,.,by = "MFD") %>% 
    transmute(MFD ,MIS03 = MIS03/Quantity)
}

You can add further logic to handle other cases. As a note, you do not need to use the & operator in dplyr's filter function as comma separated arguments are automatically handled in that manner.

As for needing both & and | functionality, I would recommend adding an input so the user can choose (If that's what you want) and add more logic similar to above so that different filtering operations are happening based on what the user wants.

thanks it worked :smile:

1 Like

If your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: