-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
328 lines (302 loc) · 14.1 KB
/
server.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
library(leaflet)
library(rgdal)
library(RColorBrewer)
library(scales)
library(lattice)
library(tidyverse)
# Leaflet bindings are a bit slow; for now we'll just sample to compensate
# set.seed(100)
zipdata <- allzips[sample.int(nrow(allzips), 10000),]
# # By ordering by centile, we ensure that the (comparatively rare) SuperZIPs
# # will be drawn last and thus be easier to see
# zipdata <- zipdata[order(zipdata$centile),]
function(input, output, session) {
## Interactive Map ###########################################
colRow <- readOGR(".", "GLOBIOM_Grid_Prov_Rev4")
# Create the map
output$map <- renderLeaflet({
leaflet() %>%
addTiles(
urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
) %>%
setView(lng = 124.0832, lat = -2.629957, zoom = 5)
})
# A reactive expression that returns the set of zips that are
# in bounds right now
# zipsInBounds <- reactive({
# if (is.null(input$map_bounds))
# return(zipdata[FALSE,])
# bounds <- input$map_bounds
# latRng <- range(bounds$north, bounds$south)
# lngRng <- range(bounds$east, bounds$west)
#
# subset(zipdata,
# latitude >= latRng[1] & latitude <= latRng[2] &
# longitude >= lngRng[1] & longitude <= lngRng[2])
# })
seriesInView <- reactive({
typeBy <- input$type
scenBy <- input$scen
if(typeBy != "PriFor" & typeBy != "CrpLnd")
colorBy <- input$color else
colorBy <- "Area"
if(colorBy == "Area")
unit <- "Ha" else if(colorBy == "Production")
unit <- "Ton" else
unit <- "Ton/Ha"
# reshaping the table into appropriate form: year, scen, value
plotTable <- colRowRds %>% filter(Type == paste0(typeBy) & (Scen == paste0(scenBy) | Scen == "Baseline"))
if(colorBy != "Productivity"){
plotTable <- plotTable %>% select(Scen, Year, colorBy) %>% group_by(Scen, Year) %>% summarize_at(colorBy, funs(sum))
} else{
plotTable <- plotTable %>% select(Scen, Year, colorBy) %>% group_by(Scen, Year) %>% summarize_at(colorBy, funs(mean))
}
return(plotTable)
# ADhere
})
# Precalculate the breaks we'll need for the two histograms
centileBreaks <- hist(plot = FALSE, allzips$centile, breaks = 20)$breaks
# output$histCentile <- renderPlot({
# # If no zipcodes are in view, don't plot
# if (nrow(zipsInBounds()) == 0)
# return(NULL)
#
# hist(zipsInBounds()$centile,
# breaks = centileBreaks,
# main = "SuperZIP score (visible zips)",
# xlab = "Percentile",
# xlim = range(allzips$centile),
# col = '#00DD00',
# border = 'white')
# })
# output$scatterCollegeIncome <- renderPlot({
# # If no zipcodes are in view, don't plot
# if (nrow(zipsInBounds()) == 0)
# return(NULL)
#
# print(xyplot(income ~ college, data = zipsInBounds(), xlim = range(allzips$college), ylim = range(allzips$income)))
# })
observe({
typeIn <- as.character(input$type)
if(typeIn == "PriFor" | typeIn == "CrpLnd")
updateSelectInput(session, "color", "Color", choices = c("Area" = "Area"), selected = "Area") else
updateSelectInput(session, "color", "Color", choices = c(
"Production" = "Production",
"Productivity" = "Productivity",
"Area" = "Area"
))
})
# This observer is responsible for maintaining the circles and legend,
# according to the variables the user has chosen to map to color and size.
observe({
typeBy <- input$type
scenBy <- input$scen
yearBy <- input$year
if(typeBy != "PriFor" & typeBy != "CrpLnd"){
colorBy <- input$color
} else{
colorBy <- "Area"
updateSelectInput(session, "color", "Color", choices = c("Area" = "Area"), selected = "Area")
}
if(colorBy == "Area")
unit <- "(Ha)" else if(colorBy == "Production")
unit <- "(Tonnes)" else
unit <- "(Tonnes/Ha)"
# sizeBy <- input$size
# dummy data AD====
# joinTable <- read.csv("master_data_wide.csv", stringsAsFactors= FALSE)
joinTable <- colRowRds %>% filter(Type == paste0(typeBy) & Year == yearBy & Scen == paste0(scenBy))
joinTable <- joinTable %>% rename_at(1, ~"GRID")
colRowDisp <- colRowShp %>% left_join(joinTable, by = "GRID")
# colRowDisp <- colRowDisp %>% mutate(Productivity= case_when(is.na(Productivity) ~ 0, TRUE ~ Productivity))
# if (colorBy == "superzip") {
# # Color and palette are treated specially in the "superzip" case, because
# # the values are categorical instead of continuous.
# colorData <- ifelse(zipdata$centile >= (100 - input$threshold), "yes", "no")
# pal <- colorFactor("viridis", colorData)
# } else {
# colorData <- zipdata[[colorBy]]
# pal <- colorBin("viridis", colorData, 7, pretty = FALSE)
# }
# if (sizeBy == "superzip") {
# # Radius is treated specially in the "superzip" case.
# radius <- ifelse(zipdata$centile >= (100 - input$threshold), 30000, 3000)
# } else {
# radius <- zipdata[[sizeBy]] / max(zipdata[[sizeBy]]) * 30000
# }
# leafletProxy("map", data = zipdata) %>%
# clearShapes() %>%
# addCircles(~longitude, ~latitude, radius=radius, layerId=~zipcode,
# stroke=FALSE, fillOpacity=0.4, fillColor=pal(colorData)) %>%
# addLegend("bottomleft", pal=pal, values=colorData, title=colorBy,
# layerId="colorLegend")
# AD misc====
colorDat <- colRowDisp %>% dplyr::select(colorBy) %>% pull()
pale <- colorNumeric("viridis", colorDat)
palet <- colRowDisp %>% dplyr::select(colorBy) %>% pull() %>% pale()
# AD misc \ends----
# , layerId = ~GRID to be added with proper ID
# if(colorBy != "Productivity"){
leafletProxy("map", data = colRow) %>% clearShapes() %>% addPolygons(weight = 1, smoothFactor = 0.5, layerId = ~LocID,
opacity = 1.0, fillOpacity = 0.5,
color = ~palet,
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE)) %>% addLegend("bottomleft", pal=pale, na.label = "N\\A", values=colorDat, title= paste0(colorBy, "<br>", unit), layerId = "colorLegend")
# } else{
# leafletProxy("map", data = colRow) %>% clearShapes() %>% addPolygons(weight = 1, smoothFactor = 0.5, layerId = ~LocID,
# opacity = 1.0, fillOpacity = 0.5,
# color = ~palet,
# highlightOptions = highlightOptions(color = "white", weight = 2,
# bringToFront = TRUE)) %>% addLegend("bottomleft", pal=pale, na.label = "N\\A", labFormat = labelFormat(suffix = " "), values=colorDat, title= paste0(colorBy, "<br>", unit), layerId = "colorLegend", className = "panel panel-default legend leaflet-control")
# labFormat = labelFormat(big.mark = ".", decimal.mark = ",")
# }
})
# leafletProxy("map", data = colRow) %>% clearShapes() %>% addPolygons(weight = 1, smoothFactor = 0.5, layerId = ~LocID,
# opacity = 1.0, fillOpacity = 0.5,
# color = ~palet,
# highlightOptions = highlightOptions(color = "white", weight = 2,
# bringToFront = TRUE)) %>% addLegend("bottomleft", pal=pale, na.label = "N\\A", values=colorDat, title= paste0(colorBy, "<br>", unit), layerId = "colorLegend")
# reactive values to avoid error in renderplot below====
yAxis <- reactive({
if(input$type == "PriFor" | input$type == "CrpLnd")
return("Area") else
return(input$color)
})
plotTitle <- reactive({
expands <- c(
"Cocoa" = "Ccoa",
"Coffee" = "Coff",
"Corn" = "Corn",
"Crop Land" = "CrpLnd",
"Primary Forest" = "PriFor",
"Rice" = "Rice",
"Small-holder Oil Plam" = "SOpal",
"Company Oil Palm" = "LOpal"
)
plothead <- expands[which(grepl(input$type, expands))] %>% names()
return(plothead)
})
plotUnit <- reactive({
if(yAxis() == "Area")
unit <- "Ha" else if(yAxis() == "Production")
unit <- "Tonnes" else
unit <- "Tonnes/Ha"
return(unit)
})
# reactive values... \ends----
output$scatterCollegeIncome <- renderPlot({
ggplot(data = seriesInView(), aes_string("Year", paste0(yAxis()))) + geom_line(aes(color = Scen)) + theme_classic() + ggtitle(plotTitle()) + ylab(plotUnit()) + theme(legend.position= "bottom", legend.title = element_blank(), axis.title.x = element_blank(), plot.title = element_text(hjust = 0.41, size = 18)) + scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))
# isolate({input$color})
})
# observe({ # sensitive only to the adjustments made in type, scenario, and color
# typeBy <- input$type
# scenBy <- input$scen
# if(typeBy != "PriFor" & typeBy != "CrpLnd")
# colorBy <- input$color else
# colorBy <- "Area"
#
# })
# Show a popup at the given location
# showZipcodePopup <- function(zipcode, lat, lng) {
# selectedZip <- allzips[allzips$zipcode == zipcode,]
# content <- as.character(tagList(
# tags$h4("Score:", as.integer(selectedZip$centile)),
# tags$strong(HTML(sprintf("%s, %s %s",
# selectedZip$city.x, selectedZip$state.x, selectedZip$zipcode
# ))), tags$br(),
# sprintf("Median household income: %s", dollar(selectedZip$income * 1000)), tags$br(),
# sprintf("Percent of adults with BA: %s%%", as.integer(selectedZip$college)), tags$br(),
# sprintf("Adult population: %s", selectedZip$adultpop)
# ))
# leafletProxy("map") %>% addPopups(lng, lat, content, layerId = zipcode)
# }
showZipcodePopup <- function(zipcode, lat, lng, colRowDisp) {
selectedZip <- colRowDisp[colRowDisp$LocID == zipcode,]
content <- as.character(tagList(
tags$h4("Province: ", paste0(capwords(selectedZip$PROVINSI, strict = TRUE))),
# tags$strong(HTML(sprintf("%s, %s %s",
# selectedZip$city.x, selectedZip$state.x, selectedZip$zipcode
# ))), tags$br(),
sprintf("Production (Tonnes): %s", prettyNum(round(selectedZip$Production, 3), big.mark = ",")), tags$br(),
sprintf("Area (Ha): %s", prettyNum(round(selectedZip$Area, 3), big.mark = ",")), tags$br(),
sprintf("Productivity (Tonnes/Ha): %s", prettyNum(round(selectedZip$Productivity, 4), big.mark = ","))
))
leafletProxy("map") %>% addPopups(lng, lat, content, layerId = zipcode)
}
# When map is clicked, show a popup with city info
observe({
leafletProxy("map") %>% clearPopups()
typeBy <- input$type
scenBy <- input$scen
yearBy <- input$year
if(typeBy != "PriFor" & typeBy != "CrpLnd")
colorBy <- input$color else
colorBy <- "Area"
joinTable <- colRowRds %>% filter(Type == paste0(typeBy) & Year == yearBy & Scen == paste0(scenBy))
joinTable <- joinTable %>% rename_at(1, ~"GRID")
colRowDisp <- colRowShp %>% left_join(joinTable, by = "GRID")
event <- input$map_shape_click
if (is.null(event))
return()
isolate({
showZipcodePopup(event$id, event$lat, event$lng, colRowDisp = colRowDisp)
})
})
## Data Explorer ###########################################
# observe({
# cities <- if (is.null(input$states)) character(0) else {
# filter(cleantable, State %in% input$states) %>%
# `$`('City') %>%
# unique() %>%
# sort()
# }
# stillSelected <- isolate(input$cities[input$cities %in% cities])
# updateSelectInput(session, "cities", choices = cities,
# selected = stillSelected)
# })
#
# observe({
# zipcodes <- if (is.null(input$states)) character(0) else {
# cleantable %>%
# filter(State %in% input$states,
# is.null(input$cities) | City %in% input$cities) %>%
# `$`('Zipcode') %>%
# unique() %>%
# sort()
# }
# stillSelected <- isolate(input$zipcodes[input$zipcodes %in% zipcodes])
# updateSelectInput(session, "zipcodes", choices = zipcodes,
# selected = stillSelected)
# })
#
# observe({
# if (is.null(input$goto))
# return()
# isolate({
# map <- leafletProxy("map")
# map %>% clearPopups()
# dist <- 0.5
# zip <- input$goto$zip
# lat <- input$goto$lat
# lng <- input$goto$lng
# showZipcodePopup(zip, lat, lng)
# map %>% fitBounds(lng - dist, lat - dist, lng + dist, lat + dist)
# })
# })
#
# output$ziptable <- DT::renderDataTable({
# df <- cleantable %>%
# filter(
# Score >= input$minScore,
# Score <= input$maxScore,
# is.null(input$states) | State %in% input$states,
# is.null(input$cities) | City %in% input$cities,
# is.null(input$zipcodes) | Zipcode %in% input$zipcodes
# ) %>%
# mutate(Action = paste('<a class="go-map" href="" data-lat="', Lat, '" data-long="', Long, '" data-zip="', Zipcode, '"><i class="fa fa-crosshairs"></i></a>', sep=""))
# action <- DT::dataTableAjax(session, df)
#
# DT::datatable(df, options = list(ajax = list(url = action)), escape = FALSE)
# })
}