Added date filtering + some refactoring

This commit is contained in:
Jakub Wajs 2019-05-15 22:28:34 +02:00
parent 06ca590748
commit 83b050d711
5 changed files with 61 additions and 16 deletions

View File

@ -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()
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()

View File

@ -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)

View File

@ -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)
)

View File

@ -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)
)
}

1
app.R
View File

@ -10,6 +10,7 @@
library(shiny)
library(dplyr)
library(shinythemes)
library(lubridate)
# Load files
source("Data/Loader.R")