Home > Software design >  filter() function having issues in filtering rows using selectinput(multiple)
filter() function having issues in filtering rows using selectinput(multiple)

Time:02-11

The following code filters rows of CO2 with specific names from the 'plant' column. However i have been having trouble doing this. When i use selectinput i dont get all the rows. For example in the following picture i have selected so many types of plants but only a few rows show up. enter image description here

 library(shiny)
    library(tidyverse)
    ui <- fluidPage((fluidRow(column(6,
        selectInput('co2',choices=CO2[1],multiple = T),
        tableOutput('table'))
        
       
        
        
        )
       
        
        
        )
        
    )
    
    server <- function(input, output, session) {
        output$table <-renderTable(
            filter(CO2,Plant==input$co2))
        
    }
    
    shinyApp(ui, server)

CodePudding user response:

I made your example reproducible. Does this solve your problem ?

library(shiny)
library(tidyverse)

CO2 <- data.frame(Plant=c("id1","id2","id3"),Info=c("plant1","plant2","plant3"))

ui <- fluidPage((fluidRow(column(6,
                                 selectInput('co2',"selecting",choices=CO2$Plant,multiple = T),
                                 tableOutput('table')))))

server <- function(input, output, session) {
  output$table <-renderTable(
    if(!length(input$co2)==0){
      CO2 %>% dplyr::filter(Plant %in% input$co2)
  } else CO2
  )
}

shinyApp(ui, server)

I think the error was to use the operator == and not %in%, which made the behavior of your filter quite strange.

  • Related