TPD-InternationalFootballRe.../.Rproj.user/812E148F/sources/s-83ED6DC2/42023A5F-contents
2019-05-14 22:41:05 +02:00

88 lines
2.1 KiB
Plaintext

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
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
)
)
}
# Run the application
shinyApp(ui = ui, server = server)