It's not really enough to test lines of code from your server function individually (though that's a good place to start!) — since they act together, you need to look at what the whole expression block produces together.
I'm not totally clear on what you want your table to look like — is it grouped by league or by team? is there more than one row, once the user has filtered it? It would help if you could provide an example. However, here's one way I might make a similar table outside of shiny:
suppressPackageStartupMessages(library(tidyverse))
football_data <- tribble(
~league_name, ~season, ~home_team, ~away_team,
"League A", "Season 1", "Team A1", "Team A2",
"League A", "Season 1", "Team A1", "Team A3",
"League A", "Season 1", "Team A2", "Team A1",
"League A", "Season 1", "Team A2", "Team A3",
"League A", "Season 1", "Team A3", "Team A1",
"League A", "Season 1", "Team A3", "Team A2",
"League B", "Season 1", "Team B1", "Team B2",
"League B", "Season 1", "Team B1", "Team B3",
"League B", "Season 1", "Team B2", "Team B1",
"League B", "Season 1", "Team B2", "Team B3",
"League B", "Season 1", "Team B3", "Team B1",
"League B", "Season 1", "Team B3", "Team B2",
"League A", "Season 2", "Team A1", "Team A2",
"League A", "Season 2", "Team A1", "Team A3",
"League A", "Season 2", "Team A2", "Team A1",
"League B", "Season 2", "Team B2", "Team B3",
"League B", "Season 2", "Team B3", "Team B1",
"League B", "Season 2", "Team B3", "Team B2")
# stand-in for user-selected input
input <- list(league = "League A")
football_data %>%
filter(league_name == input$league) %>%
gather(key = "playing_as", value = "team", home_team, away_team) %>%
group_by(season) %>%
count(team) %>%
rename(matches = n)
#> # A tibble: 6 x 3
#> # Groups: season [2]
#> season team matches
#> <chr> <chr> <int>
#> 1 Season 1 Team A1 4
#> 2 Season 1 Team A2 4
#> 3 Season 1 Team A3 4
#> 4 Season 2 Team A1 3
#> 5 Season 2 Team A2 2
#> 6 Season 2 Team A3 1
Once I know that my code works to generate the table I want, I can work on dropping it into my shiny app. An extremely simple toy example:
library(tidyverse)
library(shiny)
library(DT)
# create football_data object by reading in data... left this part out to
# keep this example brief, but can use same tribble statement as above
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("league", "Select league", unique(football_data$league_name))
),
mainPanel(
DT::dataTableOutput("matches_by_team")
)
)
),
server = function(input, output) {
output$matches_by_team <- DT::renderDataTable({
# here's that same block I tested outside of my shiny app
football_data %>%
gather(key = "playing_as", value = "team", home_team, away_team) %>%
group_by(season) %>%
filter(league_name == input$league) %>%
count(team) %>%
rename("matches" = n)
})
}
)