Dataframe not filtering properly in shiny


#1

I have created a github


I'm hoping someone could help me with my issue. I'm a real newbie..


#2

When filtering by skater position, you need to use %in% rather than == because input$posInput can include multiple values (i.e., vector > length 1). I also included an arrange line to sort GS.GP in descending order. Because your input data is already sorted that way, it isn't strictly necessary because the filtering doesn't change the order, but if your app gets more complicated then it is good to make sure the ordering is what you want. You can also use DataTables to give your users sort options. Anyway, here is your server function with my small changes.

server <- function(input, output) {
  output$results <- renderTable({
    filtered <-
      gs %>%
      filter(Age >= input$ageInput[1],
             Age <= input$ageInput[2],
             Pos %in% input$posInput
      ) %>% 
      arrange(-GS.GP)
    filtered
  })
}

#3

HI Travis,
Thank you so much!

Clair


#4

Hi Travis,
I added a pickerInput with multiple select/deselect options and of course because of my limited coding knowledge I can't get my renderTable to produce what I want. It only returns data for the choice of SSM. I have tried

Team %in% input$teams
Team == input$teams

Both only gave me results for the team "SSM"

I have updated my github with the new code.

Thanks if you have time to help....


#5

The gs data frame has abbreviated names for teams, but input$teams uses longer names (except for SSM). You can use named vectors to have pickerInput display a different value to the user than what is passed to the server (see example at bottom of this help page).

In your case, because you have so many teams, you might want to create a lookup table for your team abbreviations and full names. Then you can left_join your lookup table to your gamescore data frame. Just to be clear, I'm suggesting this as a pre-processing step, not something that you include in your shiny app.

If you use the left join approach, you can use the full team name in your pickerInput. Last, rather than typing out the vector, you can use sort(unique(gs$FullTeamName)) as the value for your choices and selected arguments of pickerInput.