-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
113 lines (95 loc) · 3.72 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
pacman::p_load(shiny, ggplot2, dplyr, sf, leaflet.extras, leaflet, shinythemes, ggthemes, extrafont)
# load data from data/
mo_flips_longlat <- readRDS("data/mo_flips_longlat.RDS")
cd_2013_mo <- readRDS("data/cd_2013_mo.RDS")
state_2013_mo <- readRDS("data/state_2013_mo.RDS")
# setting up color palettes for leaflet map
colfunc <- colorRampPalette(c("#bfd3e6", "#6e016b"))
bins <- c(0, 1, 4, 7, 9) # hard coded for now, fix later
pal <- colorBin(colfunc(4), domain = mo_flips_longlat$nodeFlips, bins = bins)
labels <- ifelse(mo_flips_longlat$nodeFlips == 1,
sprintf(
"<strong>%s</strong><br/>Congressional district %s<br/>%g flip",
mo_flips_longlat$NAMELSAD10, mo_flips_longlat$CD, mo_flips_longlat$nodeFlips
) %>% lapply(htmltools::HTML),
sprintf(
"<strong>%s</strong><br/>Congressional district %s<br/>%g flips",
mo_flips_longlat$NAMELSAD10, mo_flips_longlat$CD, mo_flips_longlat$nodeFlips
) %>% lapply(htmltools::HTML))
ui <- shinyUI(fluidPage(
theme = shinytheme("cosmo"),
tags$head(
tags$style(HTML(".leaflet-container { background: #F2EFE9; }")) # set the background color
),
titlePanel("Markov Chain Flips: Missouri VTDs"),
sidebarLayout(
sidebarPanel(width = 3,
helpText("Select the number of bins to display on the histogram below."),
numericInput("num",
h3("Bins:"),
value = 10),
plotOutput("histogram")),
mainPanel(
fluidRow(
splitLayout(leafletOutput("mymap", height = 800))
)
)
)
)
)
server <- function(input, output, session) {
output$histogram <- renderPlot({
ggplot(mo_flips_longlat, aes(x = nodeFlips)) +
geom_histogram(bins = input$num, fill = "#A48DBD", color = "white") +
theme_hc() +
labs(y = "Number of VTDs",
x = "Number of district flips") +
theme(text = element_text(family = "Century Gothic"))
})
output$mymap <- renderLeaflet({
leaflet(data = mo_flips_longlat) %>%
addPolygons(
fillColor = ~pal(nodeFlips),
opacity = 1,
color = 'white',
weight = .5,
fillOpacity = 0.75,
smoothFactor = 0,
highlightOptions = highlightOptions(
color = "darkgray",
weight = 2,
bringToFront = FALSE
),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addSearchOSM(options = searchFeaturesOptions(hideMarkerOnCollapse = TRUE)) %>%
addResetMapButton() %>%
addLegend("bottomleft",
pal = pal,
values = ~nodeFlips,
title = "Number of Flips <br>in Markov Chain",
opacity = 1,
labFormat = function(type, cuts, p) {
n = length(cuts)
paste0(cuts[-n], " to ", cuts[-1] -1)
}) %>%
addMiniMap(width = 225,
height = 225,
toggleDisplay = TRUE,
zoomAnimation = TRUE) %>%
addPolylines(data = cd_2013_mo,
color = "#7a7a7a",
weight = 2,
group = "Show 2012 Congressional Districts") %>%
addPolylines(data = state_2013_mo,
color = "#7a7a7a",
weight = 2) %>%
addLayersControl(overlayGroups = c("Show 2012 Congressional Districts"),
options = layersControlOptions(collapsed = FALSE),
position = "topright")
})
}
shinyApp(ui, server, options = list(height = 1080))