Basic statistics and map POC

This commit is contained in:
Jakub Wajs 2019-05-19 19:05:06 +02:00
parent b69c80c37b
commit 6d1c9756ef
10 changed files with 144 additions and 10 deletions

View File

@ -1,6 +1,3 @@
getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))
)
runApp()
runApp() runApp()
runApp() runApp()
runApp() runApp()
@ -510,3 +507,6 @@ runApp()
runApp() runApp()
runApp() runApp()
runApp() runApp()
version()
version
packageVersion("shiny")

View File

@ -44,7 +44,7 @@ getBalanceForTeam <- function(football_data, team) {
balance <- balance_full[(balance_full$winner %in% c(team, "Draw")),] balance <- balance_full[(balance_full$winner %in% c(team, "Draw")),]
other_sum <- sum(balance_full[!(balance_full$winner %in% c(team, "Draw")),]$n) other_sum <- sum(balance_full[!(balance_full$winner %in% c(team, "Draw")),]$n)
balance <- balance %>% balance <- balance %>%
rbind(c("Other", other_sum)) rbind(c("Loss", other_sum))
balance <- balance %>% balance <- balance %>%
mutate(n = as.numeric(as.character(n))) mutate(n = as.numeric(as.character(n)))
return(balance) return(balance)

View File

@ -0,0 +1,9 @@
#
# Interactive Map Component
#
interactiveMapStatistics <- function(input, output) {
output$interactive_world_map <- renderLeaflet(
leaflet(World) %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 17)
)
}

View File

@ -81,4 +81,11 @@ balancePercentage <- function(input, output) {
y = cumsum(percentage) - percentage/2, y = cumsum(percentage) - percentage/2,
label = percent(percentage))) label = percent(percentage)))
}) })
}
# Summary statistics
summaryStatistics <- function(input, output) {
output$summary_statistics <- renderTable(
goalsScored(getFilteredResultsForTeam(input), input$team)
)
} }

View File

@ -5,15 +5,21 @@
# Import files # Import files
source("Server/Results.R") source("Server/Results.R")
source("Server/DataProcessor.R") source("Server/DataProcessor.R")
source("Server/SummaryStatistics.R")
source("Server/InteractiveMapStatistics.R")
# Server logic # Server logic
server <- function(input, output) { server <- function(input, output) {
# Tab 1 # Tab 1
balanceForTeamBarplot(input, output) balanceForTeamBarplot(input, output)
matchesForTeam(input, output) matchesForTeam(input, output)
summaryStatistics(input, output)
# Tab 2 # Tab 2
balanceBetweenTeams(input, output) balanceBetweenTeams(input, output)
balancePercentage(input, output) balancePercentage(input, output)
resultsWithPagination(input, output) resultsWithPagination(input, output)
# Tab 3
interactiveMapStatistics(input, output)
} }

View File

@ -0,0 +1,80 @@
#
# Summary statistics
#
# Sum
goalsScored <- function(matches, team) {
home_goals <- matches %>%
filter(home_team == team) %>%
select(home_score) %>% sum()
away_goals <- matches %>%
filter(away_team == team) %>%
select(away_score) %>% sum()
goals_scored <- tribble(
~home_goals, ~away_goals, ~overall,
home_goals, away_goals, home_goals + away_goals
)
return(goals_scored)
}
goalsLost <- function(matches, team) {
home_goals <- matches %>%
filter(home_team != team) %>%
select(home_score) %>% sum()
away_goals <- matches %>%
filter(away_team != team) %>%
select(away_score) %>% sum()
goals_lost <- tribble(
~home_goals, ~away_goals, ~overall,
home_goals, away_goals, home_goals + away_goals
)
return(goals_scored)
}
matchesNumber <- function(matches, team) {
home_matches <- matches %>%
filter(home_team == team) %>% nrow()
away_matches <- matches %>%
filter(away_team == team) %>% nrow()
matches_number <- tribble(
~home_matches, ~away_matches, ~overall,
home_matches, away_matches, home_matches + away_matches
)
return(matches_number)
}
# Mean
avgGoalsScored <- function(matches, team) {
goals_scored <- goalsScored(matches, team)
matches_number <- matchesNumber(matches, team)
avg_goals_scored <- tribble(
~home_goals_avg, ~away_goals_avg, ~overall_avg,
goals_scored$home_goals / matches_number$home_matches,
goals_scored$away_goals / matches_number$away_matches,
goals_scored$overall / matches_number$overall
)
return(avg_goals_scored)
}
avgGoalsLost <- function(matches, team) {
goals_lost <- goalsLost(matches, team)
matches_number <- matchesNumber(matches, team)
avg_goals_lost <- tribble(
~home_goals_avg, ~away_goals_avg, ~overall_avg,
goals_lost$home_goals / matches_number$home_matches,
goals_lost$away_goals / matches_number$away_matches,
goals_lost$overall / matches_number$overall
)
return(avg_goals_lost)
}

View File

@ -14,4 +14,4 @@ balanceComponent <- function(pieChartSize, barplotSize)
) )
) )
) )
} }

View File

@ -0,0 +1,12 @@
#
# Interactive map component
#
interactiveMapComponent <- function(rowSize)
{
fluidRow(
column(rowSize,
leafletOutput("interactive_world_map")
)
)
}

24
UI/UI.R
View File

@ -5,6 +5,7 @@
# Import files # Import files
source("UI/SidebarPanel.R") source("UI/SidebarPanel.R")
source("UI/BalanceComponent.R") source("UI/BalanceComponent.R")
source("UI/InteractiveMapComponent.R")
# UI # UI
ui <- navbarPage( ui <- navbarPage(
@ -36,11 +37,13 @@ ui <- navbarPage(
) )
), ),
tabPanel( tabPanel(
"Summary" "Summary",
tableOutput("summary_statistics")
), ),
tabPanel( tabPanel(
"All matches", "All matches",
DT::dataTableOutput("matches_for_team") dataTableOutput("matches_for_team")
) )
) )
) )
@ -71,5 +74,20 @@ ui <- navbarPage(
), ),
# Tab 3 # Tab 3
tabPanel("World map") tabPanel("Overall",
fluidRow(
column(12,
tabsetPanel(
tabPanel(
"Map",
interactiveMapComponent(12)
),
tabPanel(
"Summary"
),
tabPanel(
"All matches"
)
))
))
) )

6
app.R
View File

@ -14,6 +14,9 @@ library(shinyWidgets)
library(lubridate) library(lubridate)
library(ggplot2) library(ggplot2)
library(scales) library(scales)
library(tmap)
library(tmaptools)
library(leaflet)
# Load files # Load files
source("Data/Loader.R") source("Data/Loader.R")
@ -21,5 +24,4 @@ source("UI/UI.R")
source("Server/Server.R") source("Server/Server.R")
# Run the application # Run the application
shinyApp(ui = ui, server = server) shinyApp(ui = ui, server = server)