Added barplot for balance between teams

This commit is contained in:
Jakub Wajs 2019-05-15 00:27:04 +02:00
parent 64ad86242b
commit 662e1bb737
6 changed files with 102 additions and 71 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata

24
Data/Loader.R Normal file
View File

@ -0,0 +1,24 @@
# Functions
get_winner_name <- function(match_row) {
if(match_row["home_score"] > match_row["away_score"]) {
return(match_row["home_team"])
}
else if(match_row["home_score"] < match_row["away_score"]) {
return(match_row["away_team"])
}
else {
return("Draw")
}
}
# 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))
# Prepare data
home_teams <- football_data %>%
select("home_team") %>% unique()
away_teams <- football_data %>%
select("away_team") %>% unique()

38
Server/Results.R Normal file
View File

@ -0,0 +1,38 @@
# 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 with pagination
resultsWithPagination <- function(input, output) {
output$results_pagination <- DT::renderDataTable(
getMatchesList(input$first_team, input$second_team, football_data),
extensions = 'Buttons',
options = list(
lengthMenu = list(c(5, 10, -1), c('5', '10', 'All')),
pageLength = 10,
searching = FALSE
)
)
}
# Balance
balanceBetweenTeams <- function(input, output) {
output$balance <- renderPlot({
barplot(
bal <- 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
)
})
}

8
Server/main.R Normal file
View File

@ -0,0 +1,8 @@
# Import files
source("Server/Results.R")
# Server logic
server <- function(input, output) {
balanceBetweenTeams(input, output)
resultsWithPagination(input, output)
}

24
UI/main.R Normal file
View File

@ -0,0 +1,24 @@
# UI
ui <- fluidPage(
# Application title
titlePanel("International football results 1872-2018"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("first_team",
"First team:",
home_teams),
selectInput("second_team",
"Second team:",
away_teams)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("balance"),
DT::dataTableOutput("results_pagination")
)
)
)

75
app.R
View File

@ -10,77 +10,10 @@
library(shiny)
library(dplyr)
# Functions
get_matches_list <- function(first_team, second_team) {
matches <- football_data %>%
select(1:9) %>%
filter(home_team %in% c(first_team, second_team)
& away_team %in% c(first_team, second_team))
return(matches)
}
get_winner_name <- function(match_row) {
if(match_row["home_score"] > match_row["away_score"]) {
return(match_row["home_team"])
}
else if(match_row["home_score"] < match_row["away_score"]) {
return(match_row["away_team"])
}
else {
return("Draw")
}
}
# 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))
# Prepare data
home_teams <- football_data %>%
select("home_team") %>% unique()
away_teams <- football_data %>%
select("away_team") %>% unique()
# UI
ui <- fluidPage(
# Application title
titlePanel("International football results 1872-2018"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("first_team",
"First team:",
home_teams),
selectInput("second_team",
"Second team:",
away_teams)
),
# Show a plot of the generated distribution
mainPanel(
#plotOutput("balance"), TODO: balance plot (W:L:D)
DT::dataTableOutput("results_pagination")
)
)
)
# Server logic
server <- function(input, output) {
#output$balance <- renderPlot()
output$results_pagination <- DT::renderDataTable(
get_matches_list(input$first_team, input$second_team),
extensions = 'Buttons',
options = list(
lengthMenu = list(c(5, 10, -1), c('5', '10', 'All')),
pageLength = 10,
searching = FALSE
)
)
}
# Load files
source("Data/Loader.R")
source("UI/main.R")
source("Server/main.R")
# Run the application
shinyApp(ui = ui, server = server)