From 553164e6bf4d783f56cc871ad894fdc72c023418 Mon Sep 17 00:00:00 2001 From: Jakub Wajs Date: Sun, 19 May 2019 01:33:04 +0200 Subject: [PATCH] Added balance for one team --- Server/DataProcessor.R | 12 ++++++++++++ Server/Results.R | 30 ++++++++++++++++++++++-------- Server/Server.R | 1 + UI/SidebarPanel.R | 2 +- UI/UI.R | 15 ++++++++++++++- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Server/DataProcessor.R b/Server/DataProcessor.R index 69ec4c4..d577cac 100644 --- a/Server/DataProcessor.R +++ b/Server/DataProcessor.R @@ -36,4 +36,16 @@ getBalance <- function(football_data) { getBalancePercentage <- function(balance) { balancePerc <- balance %>% mutate(percentage = n/sum(.$n)) +} + +getBalanceForTeam <- function(football_data, team) { + balance_full <- football_data %>% + count(winner, sort = TRUE) + 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)) + balance <- balance %>% + mutate(n = as.numeric(as.character(n))) + return(balance) } \ No newline at end of file diff --git a/Server/Results.R b/Server/Results.R index 737b291..de51eea 100644 --- a/Server/Results.R +++ b/Server/Results.R @@ -5,7 +5,7 @@ # Results with pagination getFilteredResultsForTeam <- function(input) { getMatchesForTeam(football_data, input$team) %>% - filterByDate(as_date(input$date_range[1]), as_date(input$date_range[2])) + filterByDate(as_date(input$date_range_for_team[1]), as_date(input$date_range_for_team[2])) } getFilteredResults <- function(input) { @@ -38,10 +38,6 @@ matchesForTeam <- function(input, output) { } # Balance -getBalanceData <- function(input) { - getBalance(getFilteredResults(input)) -} - balanceBetweenTeams <- function(input, output) { output$balance <- renderPlot({ ggplot(getFilteredResults(input), aes(x=as.factor(winner), @@ -52,7 +48,27 @@ balanceBetweenTeams <- function(input, output) { }) } +getBalanceDataForTeamBarplot <- function(input) { + return( + getBalanceForTeam(getFilteredResultsForTeam(input), input$team) + ) +} + +balanceForTeamBarplot <- function(input, output) { + output$balance_for_team <- renderPlot({ + ggplot(getBalanceDataForTeamBarplot(input), + aes(x=as.factor(winner), y=as.factor(n))) + + geom_bar(stat = "identity", fill = rgb(0.6, 0.1, 0.4, 0.7)) + + theme_minimal() + + labs(x = "", y = "Matches balance for") + }) +} + # Balance percentage +getBalanceData <- function(input) { + getBalance(getFilteredResults(input)) +} + balancePercentage <- function(input, output) { output$balancePieChart <- renderPlot({ ggplot(getBalancePercentage(getBalanceData(input))) + @@ -65,6 +81,4 @@ balancePercentage <- function(input, output) { y = cumsum(percentage) - percentage/2, label = percent(percentage))) }) -} - -# Top teams barplot \ No newline at end of file +} \ No newline at end of file diff --git a/Server/Server.R b/Server/Server.R index 7c3472f..1614229 100644 --- a/Server/Server.R +++ b/Server/Server.R @@ -9,6 +9,7 @@ source("Server/DataProcessor.R") # Server logic server <- function(input, output) { # Tab 1 + balanceForTeamBarplot(input, output) matchesForTeam(input, output) # Tab 2 diff --git a/UI/SidebarPanel.R b/UI/SidebarPanel.R index 1d16c6b..590bd57 100644 --- a/UI/SidebarPanel.R +++ b/UI/SidebarPanel.R @@ -47,7 +47,7 @@ teamSidebarPanel <- function() { selectInput("team", "Choose team:", teams), - dateRangeInput("date_range", + dateRangeInput("date_range_for_team", "Date range:", start = min_date_from, end = max_date_to, diff --git a/UI/UI.R b/UI/UI.R index a91b198..2b7d1b4 100644 --- a/UI/UI.R +++ b/UI/UI.R @@ -20,7 +20,20 @@ ui <- navbarPage( column(9, tabsetPanel( tabPanel( - "Charts" + "Charts", + h2("Matches balance charts"), + fluidRow( + column(6, + wellPanel( + plotOutput("balance_for_team") + ) + ), + column(6, + wellPanel( + verbatimTextOutput("Test") + ) + ) + ) ), tabPanel( "Summary"