diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/Data/Loader.R b/Data/Loader.R new file mode 100644 index 0000000..a669d2a --- /dev/null +++ b/Data/Loader.R @@ -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() \ No newline at end of file diff --git a/Server/Results.R b/Server/Results.R new file mode 100644 index 0000000..bae477d --- /dev/null +++ b/Server/Results.R @@ -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 + ) + }) +} \ No newline at end of file diff --git a/Server/main.R b/Server/main.R new file mode 100644 index 0000000..8fcfa4e --- /dev/null +++ b/Server/main.R @@ -0,0 +1,8 @@ +# Import files +source("Server/Results.R") + +# Server logic +server <- function(input, output) { + balanceBetweenTeams(input, output) + resultsWithPagination(input, output) +} diff --git a/UI/main.R b/UI/main.R new file mode 100644 index 0000000..7b117b1 --- /dev/null +++ b/UI/main.R @@ -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") + ) + ) +) \ No newline at end of file diff --git a/app.R b/app.R index 941c57a..536e000 100644 --- a/app.R +++ b/app.R @@ -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)