Skip to content

Instantly share code, notes, and snippets.

@allatambov
Last active April 10, 2020 13:33
Show Gist options
  • Save allatambov/837f2f07bef8e84b04672f297e4f45e4 to your computer and use it in GitHub Desktop.
Save allatambov/837f2f07bef8e84b04672f297e4f45e4 to your computer and use it in GitHub Desktop.
#
# 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(tidyverse)
winter <- read.csv("/Users/allat/Desktop/winter.csv")
summer <- read.csv("/Users/allat/Desktop/summer.csv")
games <- rbind.data.frame(summer, winter)
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("cnt", "Country",
choices = unique(games$Country)),
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)),
"Summer" = selectInput("year", "Year:",
choices = unique(summer$Year)))
})
output$distPlot <- renderPlot({
if (input$season == 'Winter'){
games <- winter
}else{
games <- summer
}
dat <- games %>% filter(Year == req(input$year),
Country == req(input$cnt))
ggplot(data = dat, aes(x = Medal, fill = Medal)) + geom_bar() +
scale_fill_manual(values = c("#cd7f32", "lightgray", "gold")) +
theme_dark()
})
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$cnt)) %>%
select(-c(Year, Country))
head(dat, 5)
}))
output$distPlot2 <- renderPlot({
if (input$season == 'Winter'){
games <- winter
}else{
games <- summer
}
dat <- games %>% filter(Country == req(input$cnt),
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("#cd7f32", "lightgray", "gold")) +
theme_dark()
}else{
ggplot(data = counts, aes(x = Year, y = n, group = Medal, color = Medal)) + geom_line(lwd = 2) +
scale_color_manual(values = c("#cd7f32", "lightgray", "gold")) +
theme_dark()
}
})
}
# 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