-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
301 lines (260 loc) · 13.1 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
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
library(shiny)
library(bslib)
library(leaflet)
library(sf)
library(stringr)
library(dplyr)
library(shinycssloaders)
library(DT)
library(leafem)
library(tidyr)
library(ggplot2)
library(shinyWidgets)
library(markdown) # needed to use includeMarkdown()
library(leaflet.extras)
library(thematic)
library(shinyFeedback)
library(lwgeom) # needed to use sf::st_distance()
library(rnaturalearth)
# https://github.com/r-spatial/sf/issues/1762
sf::sf_use_s2(FALSE)
# Read data
# Countries
countries_sf <- st_read('data/countries.gpkg') |>
st_transform(3857) |>
st_simplify(preserveTopology = TRUE,
dTolerance=100)
# Lakes
lakes_country <- st_read("data/lakes_country.gpkg") |>
st_transform(3857)
# Sites
sites_country <- st_read("data/sites_country.gpkg")
# Links for navbar
link_neotoma <- tags$a(shiny::icon("github"),
"GitHub",
href = "https://github.com/NeotomaDB",
target = "_blank")
link_slack <- tags$a(shiny::icon("slack"),
"Slack",
href = "https://neotomadb.slack.com/ssb/redirect",
target = "_blank")
# UI
ui = bslib::page_navbar(
title = "Neotoma",
tags$head(
tags$link(rel = "stylesheet",
type = "text/css",
href = "style.css"),
tags$link(rel = "shortcut icon",
href = "water.ico", # https://gauger.io/fonticon/
type = "image/x-icon")
),
theme = bslib::bs_theme(version = 5,
bootswatch = "minty",
navbar_bg = "#1b3964",
primary = "#1b3964",
base_font = bslib::font_google("Raleway",
wght = "300"),
heading_font = bslib::font_google("Raleway",
wght = "100")),
bslib::nav_panel(title = "Lakes",
shinyFeedback::useShinyFeedback(),
bslib::layout_columns(
col_widths = c(9, 3),
row_heights = list("auto", 1),
bslib::layout_sidebar( class = 'p-0',
sidebar = bslib::sidebar(
width = 300,
title = NULL,
open = 'always',
bg = 'white',
bslib::accordion(
id = 'sidebar_accordion',
open = c('Start Here',
'User options'),
multiple = TRUE,
height = "100%",
bslib::accordion_panel(id = 'starthere',
title = 'Start Here',
icon = shiny::icon('arrow-down'),
shiny::textInput(
inputId = 'neositeid',
label = NULL,
placeholder = "Insert Neotoma siteID"),
bslib::tooltip(
shiny::actionButton('search',
'Search!'),
paste("Check sitesid in the webpage.",
"For this demo you can use site 9606",
"as an example"))),
bslib::accordion_panel(id = 'Site metadata',
title = 'Site metadata',
metadataUI('mod_metadata')),
bslib::accordion_panel(
id = 'Map options',
title = 'Map options',
shinyWidgets::materialSwitch(
inputId = "removelakes",
label = "remove HYDROlakes layer",
status = "primary"
)
),
bslib::accordion_panel(
id = 'User options',
title = 'User options',
optionsUI('mod_options')
))
), # accordion right / sidebar
bslib::accordion(
id = 'map_accordion',
open = 'How to use this app?',
height = "100%",
bslib::accordion_panel(
id = "Map",
title = "Map of the site area",
class = 'p-0',
leafletmapUI('mod_map')
),
bslib::accordion_panel(
id = "Comments",
title = "User decision",
bslib::layout_columns(
userdecisionsUI('mod_user_decisions')
)),
bslib::accordion_panel(
id = "help",
title = "How to use this app?",
class = 'p-0',
shiny::tags$img(
src = "img-app.png",
height = 'auto',
width = '100%'
),
shiny::includeMarkdown("www/help.md"),
shiny::tags$img(
src = "img-app-end.png",
height = 'auto',
width = '100%'
)
)
) # closes accordion center
), # closes layout sidebar
bslib::accordion(
id = "lakes_or_polygons",
open = c('About'),
height = "100%",
bslib::accordion_panel(
id = "lakes",
title = "Hydrolakes DB",
hydrolakesUI('mod_hydrolakes')
),
bslib::accordion_panel(
id = "polygon",
title = "Polygon",
createpolygonUI('mod_create_polygon')
),
bslib::accordion_panel(
id = "about",
title = "About",
shiny::includeMarkdown("www/logo.md")
)
) # closes accordion right
) #layout columns
), #navbar
bslib::nav_spacer(),
bslib::nav_menu(
title = "Links",
align = "right",
bslib::nav_item(link_neotoma),
bslib::nav_item(link_slack)
))
server = function(input, output, session) {
# Select site from the database
neosites_data <- shiny::eventReactive(input$search,{
neosites_data <- sites_country |>
dplyr::mutate(siteid = as.character(siteid)) |>
dplyr::filter(siteid %in% as.character(input$neositeid)) |>
sf::st_transform(3857)
return(neosites_data)
})
# Keep the closest lakes (50 km) to NeotomaDb selected point/polygon
lake_data <- shiny::eventReactive(input$search,{
lakes_country[sf::st_is_within_distance(neosites_data(),
lakes_country,
dist = 50000)[[1]],] |>
sf::st_set_crs(3857)
})
# left accordion
metadataServer('mod_metadata',
r_neosites_data = reactive({ neosites_data() }) )
values <- optionsServer('mod_options')
# central accordion
mapvalues <- leafletmapServer('mod_map',
r_neosites_data = reactive({ neosites_data() }),
r_lake_data = reactive({ lake_data() }),
nooptions = values$nooptions,
modify = values$modify,
removelakes = reactive({ input$removelakes }) )
userdecisionsServer('mod_user_decisions',
neositeid = reactive({ input$neositeid }),
modify = values$modify,
nooptions = values$nooptions,
map_draw_new_feature = mapvalues$map_draw_new_feature,
map_shape_click = mapvalues$map_shape_click,
lk_click = hydrolakes$lk_click,
polygon_sf = newpolygon$polygon_sf)
# right accordion
hydrolakes <- hydrolakesServer('mod_hydrolakes',
countries_sf = countries_sf,
r_neosites_data = reactive({ neosites_data() }),
r_lake_data = reactive({ lake_data() }),
map_shape_click = mapvalues$map_shape_click
)
newpolygon <- createpolygonServer('mod_create_polygon',
map_draw_new_feature = mapvalues$map_draw_new_feature)
shiny::observeEvent(input$search, {
shinyFeedback::hideFeedback("neositeid")
if (!(input$neositeid %in% unique(sites_country$siteid))){
shinyFeedback::feedbackDanger(
inputId = "neositeid",
show = TRUE,
text = "Please select a valid siteId"
)}
})
# Open the accordion metadata only once the user selects a siteid
shiny::observe({
if(input$search == 1){
bslib::accordion_panel_open(id = 'sidebar_accordion',
value = 'Site metadata')
bslib::accordion_panel_open(id = 'map_accordion',
value = 'Map of the site area')
bslib::accordion_panel_close(id = 'map_accordion',
value = 'How to use this app?')
bslib::accordion_panel_open(id = 'lakes_or_polygons',
value = 'Hydrolakes DB')
bslib::accordion_panel_close(id = 'lakes_or_polygons',
value = 'About')
}
if(values$modify() == "No"){
# Open the accordion to complete and submit comments once they have selected
# what they want to do with the siteid lake data
bslib::accordion_panel_open(id = 'map_accordion',
value = 'User decision')} })
shiny::observe({
shiny::req(values$nooptions())
if(values$modify() == "No" &&
values$nooptions() == "Create lake polygon"){
# Open the accordion when the user wants to create a polygon
bslib::accordion_panel_close(id = 'lakes_or_polygons',
value = 'Hydrolakes DB')
bslib::accordion_panel_open(id = 'lakes_or_polygons',
value = 'Polygon')
}else if(values$modify() == "No" &&
values$nooptions() == "Replace with HYDROLakeDB"){
bslib::accordion_panel_close(id = 'lakes_or_polygons',
value = 'Polygon')
bslib::accordion_panel_open(id = 'lakes_or_polygons',
value = 'Hydrolakes DB')}
})
}
shinyApp(ui = ui, server = server)