From 83b050d711b8a038738882b7cfe7bf985daf2eb7 Mon Sep 17 00:00:00 2001 From: Jakub Wajs Date: Wed, 15 May 2019 22:28:34 +0200 Subject: [PATCH] Added date filtering + some refactoring --- Data/Loader.R | 23 +++++++++++++++++++---- Server/DataProcessor.R | 22 ++++++++++++++++------ Server/Results.R | 19 ++++++++++++++----- UI/SidebarPanel.R | 12 +++++++++++- app.R | 1 + 5 files changed, 61 insertions(+), 16 deletions(-) diff --git a/Data/Loader.R b/Data/Loader.R index a669d2a..29d6919 100644 --- a/Data/Loader.R +++ b/Data/Loader.R @@ -12,13 +12,28 @@ get_winner_name <- function(match_row) { } # Load data -football_data <- as_tibble(read.csv("results.csv")) -football_data_ext <- football_data %>% - select(2:5) %>% mutate(winner = apply(., 1, get_winner_name)) +football_data <- as_tibble(read.csv("results.csv")) %>% + mutate(winner = apply(., 1, get_winner_name)) # Prepare data home_teams <- football_data %>% select("home_team") %>% unique() away_teams <- football_data %>% - select("away_team") %>% unique() \ No newline at end of file + select("away_team") %>% unique() + +getDateFromData <- function(matchesData) { + return( + matchesData %>% + select(date) %>% + mutate(date = as.Date(date, "%Y-%m-%d")) + ) +} + +date_values <- getDateFromData(football_data) + +min_date_from <- getDateFromData(football_data) %>% + summarise(min = min(date)) %>% pull() + +max_date_to <- getDateFromData(football_data) %>% + summarise(max = max(date)) %>% pull() \ No newline at end of file diff --git a/Server/DataProcessor.R b/Server/DataProcessor.R index 7abc766..4ae2871 100644 --- a/Server/DataProcessor.R +++ b/Server/DataProcessor.R @@ -2,14 +2,24 @@ # DATA PROCESSING # -# Functions -getMatchesList <- function(first_team, second_team, football_data) { - matches <- football_data %>% - filter(home_team %in% c(first_team, second_team) - & away_team %in% c(first_team, second_team)) - return(matches) +# Get matches +getMatchesList <- function(matchesData, first_team, second_team) { + return( + matchesData %>% + filter(home_team %in% c(first_team, second_team) + & away_team %in% c(first_team, second_team)) + ) } +# Mathes filters +filterByDate <- function(matchesData, dateFrom, dateTo) { + return( + matchesData %>% + filter(as_date(date) >= dateFrom & as_date(date) <= dateTo) + ) +} + +# Get balance getBalance <- function(football_data) { balance <- football_data %>% count(winner, sort = TRUE) diff --git a/Server/Results.R b/Server/Results.R index f90cce6..ac5b582 100644 --- a/Server/Results.R +++ b/Server/Results.R @@ -3,9 +3,14 @@ # # Results with pagination +getFilteredResults <- function(input) { + getMatchesList(football_data, input$first_team, input$second_team) %>% + filterByDate(as_date(input$date_from), as_date(input$date_to)) +} + resultsWithPagination <- function(input, output) { output$results_pagination <- DT::renderDataTable( - getMatchesList(input$first_team, input$second_team, football_data), + getFilteredResults(input), extensions = 'Buttons', options = list( lengthMenu = list(c(5, 10, -1), c('5', '10', 'All')), @@ -16,13 +21,17 @@ resultsWithPagination <- function(input, output) { } # Balance +getBalanceData <- function(input) { + getBalance(getFilteredResults(input)) +} + balanceBetweenTeams <- function(input, output) { output$balance <- renderPlot({ barplot( - getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))$n, + getBalanceData(input)$n, main = "Balance between teams", col = rainbow(3), - names.arg = getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))$winner + names.arg = getBalanceData(input)$winner ) }) } @@ -32,11 +41,11 @@ balancePercentage <- function(input, output) { output$balancePieChart <- renderPlot({ pie( getBalancePercentage( - getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext)) + getBalanceData(input) )$percentage, main = "Match balance (percentage)", labels = getBalancePercentage( - getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext)) + getBalanceData(input) )$winner, col = rainbow(3) ) diff --git a/UI/SidebarPanel.R b/UI/SidebarPanel.R index be867cc..5c6168a 100644 --- a/UI/SidebarPanel.R +++ b/UI/SidebarPanel.R @@ -8,6 +8,16 @@ teamsSidebarPanel <- function() home_teams), selectInput("second_team", "Second team:", - away_teams) + away_teams), + dateInput("date_from", + "Date from:", + value = min_date_from, + min = min_date_from, + max = max_date_to), + dateInput("date_to", + "Date to:", + value = max_date_to, + min = min_date_from, + max = max_date_to) ) } \ No newline at end of file diff --git a/app.R b/app.R index df86fe5..17391a4 100644 --- a/app.R +++ b/app.R @@ -10,6 +10,7 @@ library(shiny) library(dplyr) library(shinythemes) +library(lubridate) # Load files source("Data/Loader.R")