Created
April 27, 2021 21:59
-
-
Save allatambov/da17e0634974c3d124c700bae30bc888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
summer <- read.csv("summer.csv") | |
winter <- read.csv("winter.csv") | |
games <- rbind.data.frame(summer, winter) | |
games <- games[games$Country != "", ] | |
# Define UI for application that draws a histogram | |
ui <- fluidPage( | |
# Application title | |
titlePanel("Olympic Games"), | |
# Sidebar with a slider input for number of bins | |
sidebarLayout( | |
sidebarPanel( | |
selectInput("season", | |
"Season", | |
choices = c("Summer", "Winter")), | |
uiOutput("special"), | |
selectInput("country", | |
"Country", | |
choices = sort(unique(games$Country)), | |
selected = "FRA"), | |
radioButtons("graph", "Graph", | |
choices = c("Area plot", "Line plot"), | |
selected = "Area plot") | |
), | |
# Show a plot of the generated distribution | |
mainPanel( | |
fluidRow(DT::dataTableOutput("table")), | |
fluidRow(column(6, plotOutput("distPlot")), | |
column(6, plotOutput("distPlot2"))) | |
) | |
) | |
) | |
# Define server logic required to draw a histogram | |
server <- function(input, output) { | |
output$special <- renderUI({ | |
switch(input$season, | |
"Winter" = selectInput("year", "Year", | |
choices = unique(winter$Year), | |
selected = "2012"), | |
"Summer" = selectInput("year", "Year", | |
choices = unique(summer$Year), | |
selected = "2012")) | |
}) | |
output$distPlot <- renderPlot({ | |
if (input$season == 'Winter'){ | |
games <- winter | |
}else{ | |
games <- summer | |
} | |
dat <- games %>% filter(Year == req(input$year), | |
Country == req(input$country)) | |
ggplot(data = dat, aes(x = Medal, fill = Medal)) + geom_bar() + | |
scale_fill_manual(values = c("Bronze" = "#cd7f32", | |
"Gold" = "gold", | |
"Silver" = "lightgray")) + | |
theme_bw() | |
}) | |
output$distPlot2 <- renderPlot({ | |
if (input$season == 'Winter'){ | |
games <- winter | |
}else{ | |
games <- summer | |
} | |
dat <- games %>% filter(Country == req(input$country), | |
Year >= as.numeric(input$year) - 15, | |
Year <= as.numeric(input$year) + 15) | |
counts <- dat %>% group_by(Year, Medal) %>% tally | |
if (input$graph == 'Area plot'){ | |
ggplot(data = counts, | |
aes(x = Year, y = n, group = Medal, fill = Medal)) + | |
geom_area() + | |
scale_fill_manual(values = c("Bronze" = "#cd7f32", | |
"Gold" = "gold", | |
"Silver" = "lightgray")) + | |
theme_bw() + | |
scale_x_continuous(breaks = sort(counts$Year)) | |
}else{ | |
ggplot(data = counts, aes(x = Year, y = n, group = Medal, color = Medal)) + geom_line(lwd = 2) + | |
scale_color_manual(values = c("Bronze" = "#cd7f32", | |
"Gold" = "gold", | |
"Silver" = "lightgray")) + | |
theme_bw() + scale_x_continuous(breaks = sort(counts$Year)) | |
} | |
}) | |
output$table <- DT::renderDataTable(DT::datatable({ | |
if (input$season == 'Winter'){ | |
games <- winter | |
}else{ | |
games <- summer | |
} | |
dat <- games %>% filter(Year == req(input$year), | |
Country == req(input$country)) %>% | |
select(-c(Year, Country)) | |
dat | |
})) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment