-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
227 lines (179 loc) · 6.69 KB
/
app.R
File metadata and controls
227 lines (179 loc) · 6.69 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
library(ggmap)
library(maps)
library(ggplot2)
library(dplyr)
library(zipcode)
library(plyr)
library(viridis)
library(maptools)
library(rgdal)
library(foreign)
library(classInt)
library(scales)
library(rgeos)
library(shinydashboard)
library(leaflet)
#setwd("~/Documents/R files/BDT de-identified work/Benephilly_map/data")
data(zipcode)
zipcode$zip <- as.numeric(zipcode$zip)
str(zipcode)
zips <- read.csv("data/Shapefiles_dbfedit1.csv",stringsAsFactors = FALSE)
#zips <- subset(zips, benefit_key != c("aca_qhp_pa", "pace" ,
#"head_start_pa", "wic" ,"medicaid_pa_recert", "eitc" ,"ccis_pa" ,"head_start_pa", "eitc_pa" ,
#"unemployment" , "aca_qhp_pa" , "cap_pa"))
zips[,c(1,3)] <- NULL
zips1 <- zips
colnames(zips1) <- c("zip", "benefit_key" , "clients_served")
zips1<- merge(zips1, zipcode, by='zip')
philly_shp <- readOGR("data/Zipcodes_Poly.shp")
philly_shp@data$CLIENTS_SE <- NULL
philly_shp@data$id <- rownames(philly_shp@data)
philly_shp.point <- fortify(philly_shp, region="id")
philly_shp.df <- inner_join(philly_shp.point,philly_shp@data, by="id")
philly_shp.df <- suppressWarnings(left_join(philly_shp.df, zips, by="CODE"))
philly_shp.df$clients_served <- as.numeric(philly_shp.df$clients_served)
head(philly_shp.df)
str(philly_shp.df)
philly_shp.df <- philly_shp.df[,c(1,2,7,9,11,12)]
ggplot(philly_shp.df, aes(long, lat, group=group )) + geom_polygon()
zips2 <- zips1[! zips1$zip %in% c(19105,19193, 19102),]
zips1b <- zips1[,1:3]
bounds<-bbox(philly_shp)
## custom label format function
myLabelFormat = function(..., reverse_order = FALSE){
if(reverse_order){
function(type = "numeric", cuts){
cuts <- sort(cuts, decreasing = T)
}
}else{
labelFormat(...)
}
}
ui <- dashboardPage(
header<-dashboardHeader(title="Benephilly Clients Served by Zipcode",
titleWidth = 600),
dashboardSidebar(disable = TRUE),
body<-dashboardBody(
fluidRow(
column(width = 5,
box(width = NULL, solidHeader = TRUE,
leafletOutput("leafMap", height=400)
)
),
column(width=4,box(width = NULL, solidHeader = TRUE,
plotOutput("map", height=400)
)),
column(width=3,
box(width=NULL,
img(src="bdtlogo.png", width="100%", height=100, align="center"),
uiOutput("BenefitOutput"))),
fluidRow(column(width=9,
box(width=NULL,
dataTableOutput("results")
)
))
)
)
)
server <- function(input, output, session) {
output$BenefitOutput <- renderUI({
selectInput("BenefitInput", "Choose a Benefit you want to map:",
sort(unique(zips2$benefit_key)),
selected = "snap")})
filtered <- reactive({
if (is.null(input$BenefitInput)) {
return(NULL)
}
philly_shp.df %>%
filter(benefit_key== input$BenefitInput ,
CODE==CODE,
clients_served== clients_served,
group==group,
long==long,
lat==lat)})
tabledata <- reactive({
if (is.null(input$BenefitInput)) {
return(NULL)
}
zips1b %>%
filter(benefit_key== input$BenefitInput ,
zip==zip,
clients_served== clients_served)})
output$map <- renderPlot({
if (is.null(filtered())) {
return()
}
ggplot() +
geom_polygon(data = philly_shp.df,
aes(x = long, y = lat, group = group),
color = "black", size = 0.25) +
geom_polygon(data = filtered(),
aes(x = long, y = lat, group = group, fill = clients_served),
color = "black", size = 0.25) +
scale_fill_distiller(name="Clients Served", palette = "YlGn", breaks = pretty_breaks(n = 6))+
theme_nothing(legend = TRUE)+
geom_text(data=zips2, aes(longitude,latitude,label=zip), color="grey",
size=2,fontface="bold")+
labs(title="Clients Served by Zipcode")
})
output$results <- renderDataTable(tabledata())
#leaflet portion
getDataSet<-reactive({
# Get a subset of the income data which is contingent on the input variables
zips_subset<- subset(zips1b,benefit_key==input$BenefitInput)
zips_subset <- rename(zips_subset,c('zip'='CODE'))
# Copy our GIS data
joinedDataset<-philly_shp
# Join the two datasets together
joinedDataset@data <- suppressWarnings(left_join(joinedDataset@data, zips_subset, by="CODE"))
joinedDataset@data$clients_served <- as.numeric(joinedDataset@data$clients_served)
#joinedDataset$clients_served[is.na(joinedDataset$clients_served)] <- 0
#joinedDataset$benefit_key[is.na(joinedDataset$benefit_key)] <- input$BenefitInput
joinedDataset@data <- na.omit(joinedDataset@data)
joinedDataset
})
output$leafMap<-renderLeaflet({
leaflet() %>%
addTiles() %>%
# Centre the map in the middle of our co-ordinates
setView(mean(bounds[1,]),
mean(bounds[2,]),
zoom=10 # set to 10 as 9 is a bit too zoomed out
)
})
observe({
theData<-getDataSet()
# colour palette mapped to data
pal <- colorQuantile(rev("YlGn"), theData$clients_served, n = 6)
# set text for the clickable popup labels
philly_popup <-paste0("<strong> Zipcodes </strong>",
theData$CODE,
"<br><strong> Clients served per zipcode </strong>",
theData$clients_served)
# If the data changes, the polygons are cleared and redrawn, however, the map (above) is not redrawn
leafletProxy("leafMap", data = theData) %>%
clearShapes() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = theData,
fillColor = pal(theData$clients_served),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 2,
popup = philly_popup)
})
observe({
theData <- getDataSet()
pal2 <- colorNumeric("YlGn", theData$clients_served, n=6)
proxy <- leafletProxy("leafMap", data = theData)
# Remove any existing legend, and only if the legend is
# enabled, create a new one.
proxy %>% clearControls()
pal <- pal2
proxy %>% addLegend(position = "bottomright",
pal = pal, values = ~clients_served , opacity = 1,
title = 'Legend',
labFormat = myLabelFormat(reverse_order = T)
)
})
}
shinyApp(ui = ui, server = server)