Added pie chart

This commit is contained in:
Jakub Wajs 2019-05-15 19:56:07 +02:00
parent d7e82ad610
commit 06ca590748
5 changed files with 47 additions and 14 deletions

22
Server/DataProcessor.R Normal file
View File

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

View File

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

View File

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

View File

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

1
app.R
View File

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