This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
197 lines (150 loc) · 7.32 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
shinyServer(function(input, output, session) {
# Get inputs: -------------------------------------------------------------------
## Conservation values (rasters) ----
user_raster <- reactive({ as.character(input$raster_selection) })
## PMP user shapefile upload ----
user_pmp_upload_path <- reactive({input$upload_pmp})
user_pmp <- read_shp(user_pmp_upload_path)
## Map click
map_click <- reactive({input$ncc_map_shape_click})
## Disable extraction run ----
shinyjs::disable("extractions_mod1-run_extractions")
shinyjs::disable("report_mod1-run_report")
shinyjs::disable("compare_tbl")
shinyjs::disable("compare_plt")
## Comparison model ----
modal_trigger <- reactive({ list(input$compare_tbl,input$compare_plt) })
compare_tbl <- reactive({ input$compare_tbl })
compare_plt <- reactive({ input$compare_plt })
# Initialize leaflet map: ------------------------------------------------------
output$ncc_map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Esri.WorldTopoMap, group = "Topographic") %>%
addProviderTiles(providers$Esri.WorldImagery, group = "Imagery") %>%
addProviderTiles(providers$Esri.WorldStreetMap, group = "Streets") %>%
fitBounds(-141.00002, 41.68132, -52.68001, 76.59341) %>%
addSidebar(id = "map_sidebar",
options = list(position = "right", fit = FALSE)) %>%
addMiniMap(tiles = providers$Esri.WorldStreetMap, toggleDisplay = T,
position = "bottomleft") %>%
addMapPane("pmp_pane", zIndex = 600) %>% # Always top layer
## Add project mgmt. polygon ----
addPolygons(data = PMP_sub,
layerId = ~id, # click event id selector
group = "Project Mgmt. Plan",
fillColor = "#33862B",
color = "black",
weight = 1,
fillOpacity = 0.7,
label = ~htmlEscape(NAME),
popup = PMP_popup(PMP_sub), # fct_popup.R
options = pathOptions(pane = "pmp_pane"),
highlightOptions = highlightOptions(weight = 3,
color = '#00ffd9')) %>%
addLayersControl(overlayGroups = c("Project Mgmt. Plan"),
baseGroups = c("Streets", "Imagery", "Topographic"),
position = "bottomleft",
options = layersControlOptions(collapsed = F))
})
# Listen for map click | pre-loaded PMPs: --------------------------------------
observeEvent(map_click(), {
if(is.null(map_click()$id) | map_click()$group != "Project Mgmt. Plan" ){}
else {
## PMP user selection ----
user_pmp <- PMP_tmp %>% dplyr::filter(id == as.numeric(map_click()$id))
## Generate histograms ----
shinyjs::show(id = "conditional_plots")
property_title_SERVER(id = "property_mod2", data=user_pmp)
output$Area <- plot_theme("Area_ha", user_pmp, goals_csv, "Area (ha)")
output$Forest <- plot_theme("Forest", user_pmp, goals_csv, "Forest (ha)")
output$Grassland <- plot_theme("Grassland", user_pmp, goals_csv, "Grassland (ha)")
output$Wetland <- plot_theme("Wetland", user_pmp, goals_csv, "Wetland (ha)")
output$River <- plot_theme("River", user_pmp, goals_csv, "River (km)")
output$Lakes <- plot_theme("Lakes", user_pmp, goals_csv, "Lakes (ha)")
## Generate Table ----
property_title_SERVER(id = "property_mod1", data=user_pmp)
pmp_table_SERVER(id = "pmp_table_mod1",
data = user_pmp,
attributes = pmp_attributes,
con_values = pmp_values)
}
# Close map-click
})
# Update map with conservation theme: ------------------------------------------
observeEvent(user_raster(),{
if(user_raster() != F) {
cons_pal <- colorNumeric(palette = "viridis", c(0,100))
leafletProxy("ncc_map") %>%
clearGroup(group= "convalue") %>%
addMapPane("raster_map", zIndex = 400) %>%
addTiles(urlTemplate = paste0("tiles/", user_raster(), "/{z}/{x}/{y}.png"),
options = pathOptions(pane = "raster_map"),
group = "convalue") %>%
addLegend(position = "topleft",
pal = cons_pal,
values = c(0,100),
opacity = 1,
layerId = "cons_legend",
className = "info legend leaflet-control sidelegend")
} else {
leafletProxy("ncc_map") %>%
clearGroup(group= "convalue") %>%
removeControl("cons_legend")
}
})
# Display user PMP / extract themes: -------------------------------------------
## Listen for shp upload ----
observeEvent(user_pmp_upload_path(), {
display_shp(user_pmp, "ncc_map")
# Enable extract impact themes button
shinyjs::enable("extractions_mod1-run_extractions")
})
## Extract themes to user pmp, update map ----
proxy <- leafletProxy("ncc_map")
extracted <- extractions_SERVER(id = "extractions_mod1", user_pmp,
feat_stack, spp_stack, proxy)
## Extractions completed ----
observeEvent(extracted$trigger, {
if(extracted$flag == 1){
shinyjs::enable("report_mod1-run_report")
shinyjs::enable("compare_tbl")
shinyjs::enable("compare_plt")
## Listen for map click | new PMPs: --------------------------------------
observeEvent(map_click(), {
if(is.null(map_click()$id) | map_click()$group != "User PMP" ){}
else {
user_pmp_new <- extracted$user_pmp_mean %>%
dplyr::filter(id == as.numeric(map_click()$id))
## Generate histograms ----
shinyjs::show(id = "conditional_plots")
property_title_SERVER(id = "property_mod2", user_pmp_new)
output$Area <- plot_theme("Area_ha", user_pmp_new, goals_csv, "Area (ha)")
output$Forest <- plot_theme("Forest", user_pmp_new, goals_csv, "Forest (ha)")
output$Grassland <- plot_theme("Grassland", user_pmp_new, goals_csv, "Grassland (ha)")
output$Wetland <- plot_theme("Wetland", user_pmp_new, goals_csv, "Wetland (ha)")
output$River <- plot_theme("River", user_pmp_new, goals_csv, "River (km)")
output$Lakes <- plot_theme("Lakes", user_pmp_new, goals_csv, "Lakes (ha)")
## Generate Table ----
property_title_SERVER(id = "property_mod1", data=user_pmp_new)
pmp_table_SERVER(id = "pmp_table_mod1",
data = user_pmp_new,
attributes = pmp_attributes,
con_values = pmp_values)
}
})
}})
# Comparison modal -----------------------------------------------------------
comparison_SERVER(id = "compare_mod1", modal_trigger, compare_tbl,
compare_plt, reactive(extracted$user_pmp_mean),
map_click, goals_csv)
## Clear user pmp ----
observeEvent(input$clear_pmp, {
clear_shp("upload_pmp", "ncc_map", "User PMP")
# Disable buttons
shinyjs::disable("extractions_mod1-run_extractions")
shinyjs::disable("report_mod1-run_report")
shinyjs::disable("compare_tbl")
shinyjs::disable("compare_plt")
})
# Close server: ----------------------------------------------------------------
})