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 # Load data
football_data <- as_tibble(read.csv("results.csv")) football_data <- as_tibble(read.csv("results.csv")) %>%
football_data_ext <- football_data %>% mutate(winner = apply(., 1, get_winner_name))
select(2:5) %>% mutate(winner = apply(., 1, get_winner_name))
# Prepare data # Prepare data
home_teams <- football_data %>% home_teams <- football_data %>%
select("home_team") %>% unique() select("home_team") %>% unique()
away_teams <- football_data %>% 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 # DATA PROCESSING
# #
# Functions # Get matches
getMatchesList <- function(first_team, second_team, football_data) { getMatchesList <- function(matchesData, first_team, second_team) {
matches <- football_data %>% return(
filter(home_team %in% c(first_team, second_team) matchesData %>%
& away_team %in% c(first_team, second_team)) filter(home_team %in% c(first_team, second_team)
return(matches) & 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) { getBalance <- function(football_data) {
balance <- football_data %>% balance <- football_data %>%
count(winner, sort = TRUE) count(winner, sort = TRUE)

View File

@ -3,9 +3,14 @@
# #
# Results with pagination # 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) { resultsWithPagination <- function(input, output) {
output$results_pagination <- DT::renderDataTable( output$results_pagination <- DT::renderDataTable(
getMatchesList(input$first_team, input$second_team, football_data), getFilteredResults(input),
extensions = 'Buttons', extensions = 'Buttons',
options = list( options = list(
lengthMenu = list(c(5, 10, -1), c('5', '10', 'All')), lengthMenu = list(c(5, 10, -1), c('5', '10', 'All')),
@ -16,13 +21,17 @@ resultsWithPagination <- function(input, output) {
} }
# Balance # Balance
getBalanceData <- function(input) {
getBalance(getFilteredResults(input))
}
balanceBetweenTeams <- function(input, output) { balanceBetweenTeams <- function(input, output) {
output$balance <- renderPlot({ output$balance <- renderPlot({
barplot( barplot(
getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))$n, getBalanceData(input)$n,
main = "Balance between teams", main = "Balance between teams",
col = rainbow(3), 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({ output$balancePieChart <- renderPlot({
pie( pie(
getBalancePercentage( getBalancePercentage(
getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext)) getBalanceData(input)
)$percentage, )$percentage,
main = "Match balance (percentage)", main = "Match balance (percentage)",
labels = getBalancePercentage( labels = getBalancePercentage(
getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext)) getBalanceData(input)
)$winner, )$winner,
col = rainbow(3) col = rainbow(3)
) )

View File

@ -8,6 +8,16 @@ teamsSidebarPanel <- function()
home_teams), home_teams),
selectInput("second_team", selectInput("second_team",
"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(shiny)
library(dplyr) library(dplyr)
library(shinythemes) library(shinythemes)
library(lubridate)
# Load files # Load files
source("Data/Loader.R") source("Data/Loader.R")