From 6d1c9756ef12adc3d9a7c17a181ebabcd2fb43b8 Mon Sep 17 00:00:00 2001 From: Jakub Wajs Date: Sun, 19 May 2019 19:05:06 +0200 Subject: [PATCH] Basic statistics and map POC --- .Rhistory | 6 +-- Server/DataProcessor.R | 2 +- Server/InteractiveMapStatistics.R | 9 ++++ Server/Results.R | 7 +++ Server/Server.R | 6 +++ Server/SummaryStatistics.R | 80 +++++++++++++++++++++++++++++++ UI/BalanceComponent.R | 2 +- UI/InteractiveMapComponent.R | 12 +++++ UI/UI.R | 24 ++++++++-- app.R | 6 ++- 10 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 Server/InteractiveMapStatistics.R create mode 100644 Server/SummaryStatistics.R create mode 100644 UI/InteractiveMapComponent.R diff --git a/.Rhistory b/.Rhistory index 84804d4..532cf33 100644 --- a/.Rhistory +++ b/.Rhistory @@ -1,6 +1,3 @@ -getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext)) -) -runApp() runApp() runApp() runApp() @@ -510,3 +507,6 @@ runApp() runApp() runApp() runApp() +version() +version +packageVersion("shiny") diff --git a/Server/DataProcessor.R b/Server/DataProcessor.R index d577cac..bde4309 100644 --- a/Server/DataProcessor.R +++ b/Server/DataProcessor.R @@ -44,7 +44,7 @@ getBalanceForTeam <- function(football_data, team) { balance <- balance_full[(balance_full$winner %in% c(team, "Draw")),] other_sum <- sum(balance_full[!(balance_full$winner %in% c(team, "Draw")),]$n) balance <- balance %>% - rbind(c("Other", other_sum)) + rbind(c("Loss", other_sum)) balance <- balance %>% mutate(n = as.numeric(as.character(n))) return(balance) diff --git a/Server/InteractiveMapStatistics.R b/Server/InteractiveMapStatistics.R new file mode 100644 index 0000000..477695d --- /dev/null +++ b/Server/InteractiveMapStatistics.R @@ -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) + ) +} \ No newline at end of file diff --git a/Server/Results.R b/Server/Results.R index de51eea..95c90b4 100644 --- a/Server/Results.R +++ b/Server/Results.R @@ -81,4 +81,11 @@ balancePercentage <- function(input, output) { y = cumsum(percentage) - percentage/2, label = percent(percentage))) }) +} + +# Summary statistics +summaryStatistics <- function(input, output) { + output$summary_statistics <- renderTable( + goalsScored(getFilteredResultsForTeam(input), input$team) + ) } \ No newline at end of file diff --git a/Server/Server.R b/Server/Server.R index 1614229..4940c8a 100644 --- a/Server/Server.R +++ b/Server/Server.R @@ -5,15 +5,21 @@ # Import files source("Server/Results.R") source("Server/DataProcessor.R") +source("Server/SummaryStatistics.R") +source("Server/InteractiveMapStatistics.R") # Server logic server <- function(input, output) { # Tab 1 balanceForTeamBarplot(input, output) matchesForTeam(input, output) + summaryStatistics(input, output) # Tab 2 balanceBetweenTeams(input, output) balancePercentage(input, output) resultsWithPagination(input, output) + + # Tab 3 + interactiveMapStatistics(input, output) } diff --git a/Server/SummaryStatistics.R b/Server/SummaryStatistics.R new file mode 100644 index 0000000..181a5c7 --- /dev/null +++ b/Server/SummaryStatistics.R @@ -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) +} \ No newline at end of file diff --git a/UI/BalanceComponent.R b/UI/BalanceComponent.R index e29b2c2..5d1c463 100644 --- a/UI/BalanceComponent.R +++ b/UI/BalanceComponent.R @@ -14,4 +14,4 @@ balanceComponent <- function(pieChartSize, barplotSize) ) ) ) -} +} \ No newline at end of file diff --git a/UI/InteractiveMapComponent.R b/UI/InteractiveMapComponent.R new file mode 100644 index 0000000..001ee91 --- /dev/null +++ b/UI/InteractiveMapComponent.R @@ -0,0 +1,12 @@ +# +# Interactive map component +# + +interactiveMapComponent <- function(rowSize) +{ + fluidRow( + column(rowSize, + leafletOutput("interactive_world_map") + ) + ) +} \ No newline at end of file diff --git a/UI/UI.R b/UI/UI.R index 2b7d1b4..49c862e 100644 --- a/UI/UI.R +++ b/UI/UI.R @@ -5,6 +5,7 @@ # Import files source("UI/SidebarPanel.R") source("UI/BalanceComponent.R") +source("UI/InteractiveMapComponent.R") # UI ui <- navbarPage( @@ -36,11 +37,13 @@ ui <- navbarPage( ) ), tabPanel( - "Summary" + "Summary", + tableOutput("summary_statistics") + ), tabPanel( "All matches", - DT::dataTableOutput("matches_for_team") + dataTableOutput("matches_for_team") ) ) ) @@ -71,5 +74,20 @@ ui <- navbarPage( ), # Tab 3 - tabPanel("World map") + tabPanel("Overall", + fluidRow( + column(12, + tabsetPanel( + tabPanel( + "Map", + interactiveMapComponent(12) + ), + tabPanel( + "Summary" + ), + tabPanel( + "All matches" + ) + )) + )) ) \ No newline at end of file diff --git a/app.R b/app.R index 3670ba4..c1da7c4 100644 --- a/app.R +++ b/app.R @@ -14,6 +14,9 @@ library(shinyWidgets) library(lubridate) library(ggplot2) library(scales) +library(tmap) +library(tmaptools) +library(leaflet) # Load files source("Data/Loader.R") @@ -21,5 +24,4 @@ source("UI/UI.R") source("Server/Server.R") # Run the application -shinyApp(ui = ui, server = server) - +shinyApp(ui = ui, server = server) \ No newline at end of file