diff --git a/Server/DataProcessor.R b/Server/DataProcessor.R new file mode 100644 index 0000000..7abc766 --- /dev/null +++ b/Server/DataProcessor.R @@ -0,0 +1,22 @@ +# +# 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) +} + +getBalance <- function(football_data) { + balance <- football_data %>% + count(winner, sort = TRUE) + return(balance) +} + +getBalancePercentage <- function(balance) { + balancePerc <- balance %>% + mutate(percentage = n/sum(.$n) * 100) +} \ No newline at end of file diff --git a/Server/Results.R b/Server/Results.R index bae477d..f90cce6 100644 --- a/Server/Results.R +++ b/Server/Results.R @@ -1,16 +1,6 @@ -# 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) -} - -getBalance <- function(football_data) { - balance <- football_data %>% - count(winner, sort = TRUE) - return(balance) -} +# +# RESULTS +# # Results with pagination resultsWithPagination <- function(input, output) { @@ -29,10 +19,26 @@ resultsWithPagination <- function(input, output) { balanceBetweenTeams <- function(input, output) { output$balance <- renderPlot({ barplot( - bal <- getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))$n, + getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))$n, main = "Balance between teams", col = rainbow(3), names.arg = getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))$winner ) }) +} + +# Balance percentage +balancePercentage <- function(input, output) { + output$balancePieChart <- renderPlot({ + pie( + getBalancePercentage( + getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext)) + )$percentage, + main = "Match balance (percentage)", + labels = getBalancePercentage( + getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext)) + )$winner, + col = rainbow(3) + ) + }) } \ No newline at end of file diff --git a/Server/main.R b/Server/main.R index 1753b9e..8c9ee7c 100644 --- a/Server/main.R +++ b/Server/main.R @@ -4,9 +4,11 @@ # Import files source("Server/Results.R") +source("Server/DataProcessor.R") # Server logic server <- function(input, output) { balanceBetweenTeams(input, output) + balancePercentage(input, output) resultsWithPagination(input, output) } diff --git a/UI/main.R b/UI/main.R index 18b5a45..70ee5b6 100644 --- a/UI/main.R +++ b/UI/main.R @@ -7,6 +7,7 @@ source("UI/SidebarPanel.R") # UI ui <- fluidPage( + theme = shinytheme("slate"), # Application title titlePanel("International football results 1872-2018"), @@ -18,6 +19,7 @@ ui <- fluidPage( # Main panel mainPanel( plotOutput("balance"), + plotOutput("balancePieChart"), DT::dataTableOutput("results_pagination") ) ) diff --git a/app.R b/app.R index 536e000..df86fe5 100644 --- a/app.R +++ b/app.R @@ -9,6 +9,7 @@ library(shiny) library(dplyr) +library(shinythemes) # Load files source("Data/Loader.R")