Skip to content

Instantly share code, notes, and snippets.

@mine-cetinkaya-rundel
Last active October 27, 2018 02:54
Show Gist options
  • Save mine-cetinkaya-rundel/829cdb7291b09a710efea982ad91f80e to your computer and use it in GitHub Desktop.
Save mine-cetinkaya-rundel/829cdb7291b09a710efea982ad91f80e to your computer and use it in GitHub Desktop.
Code for vote app at https://minecr.shinyapps.io/vote/
# Load packages ----------------------------------------------------------------
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)
library(fiftystater)
library(tools)
library(mapproj)
library(fontawesome)
# Create states data -----------------------------------------------------------
states <- tibble(
state_lower = unique(fifty_states$id),
state_title = toTitleCase(state_lower)
)
# Define UI --------------------------------------------------------------------
ui <- fluidPage(
theme = shinytheme("paper"),
# Row 3
fluidRow(
column(1),
column(10,
tags$div(align = "right",
a(href = "https://gist.github.com/mine-cetinkaya-rundel/829cdb7291b09a710efea982ad91f80e",
h4(fa("github", fill = "#B9BABD"))))
),
column(1)
),
# Application title
tags$div(align = "center", titlePanel("Should I vote?")),
# Row 1
fluidRow(
column(1),
column(3,
br(),
tags$div(align = "right", h5("If you are registered in"))
),
column(4,
tags$div(align = "center", selectInput("selected_state",
"Select your state:",
choices = states$state_title,
selected = "North Carolina"))
),
column(3,
br(),
tags$div(align = "left", h5("you should vote."))
),
column(1)
),
# Row 2
fluidRow(
plotOutput("map")
)
)
# Define server logic ----------------------------------------------------------
server <- function(input, output, session) {
output$map <- renderPlot({
states <- states %>%
mutate(selection = ifelse(
state_title == input$selected_state, TRUE, FALSE
))
ggplot(states, aes(map_id = state_lower)) +
geom_map(aes(fill = selection), map = fifty_states) +
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
scale_fill_manual(values = c("#B9BABD", "#2D62A3")) +
labs(x = "", y = "") +
theme_minimal() +
theme(legend.position = "none")
})
}
# Create Shiny app object ------------------------------------------------------
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment