-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
253 lines (221 loc) · 9.39 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
server <- function(input, output, session) {
# Activate thematic
# thematic::thematic_on(font = "auto")
# output$plot2 <- renderPlot({
# mtcars |>
# ggplot(aes(!!sym(input$var), !!sym(input$var2), color = factor(cyl)))
# })
#
observe({
# close sidebar when not in Home
if (!input$navBar %in% 'Home') {
sidebar_toggle(
id = "mySidebar",
open = F
)
} else {
sidebar_toggle(
id = "mySidebar",
open = T
)
}
})
# valuebox ----------------------------------------------------------------
output$my_value_box <- renderUI({
layout_columns(
value_box(
title = "% of 61 Local Health Departments (county, district, and independent) who've submitted a Local Needs Assessment",
value = paste0(sprintf('%0.0f', prop.table(table(shapefile$Status) )[[2]] * 100 ), '%'),
theme = value_box_theme(
bg = if (input$mode_toggle %in% 'dark') chfs$cols9[2] else chfs$cols9[9],
fg = if (input$mode_toggle %in% 'dark') 'white' else chfs$cols9[2]
),
showcase = icon('calendar-check'),
showcase_layout = "top right", full_screen = T, fill = T,
height = NULL # Set height of the value box
),
value_box(
title = "Number of downloadable files available in this site (some LHDs submitted more than 1 document)",
value = numberOfListings,
theme = value_box_theme(
bg = if (input$mode_toggle %in% 'dark') chfs$cols9[2] else chfs$cols9[9],
fg = if (input$mode_toggle %in% 'dark') 'white' else chfs$cols9[2]
),
showcase = icon('hashtag'),
showcase_layout = "top right", full_screen = T, fill = T,
height = NULL # Set height of the value box
)
# value_box(
# title = "Top Health Priorities",
# value = HTML("<ol>
# <li>Substance Abuse</li>
# <li>Physical Health</li>
# <li>Mental Health</li>
# <li>Access To Care</li>
# <li>Tobacco</li>
# <li>Cancer</li>
# <li>Diabetes</li>
# <li>Food Security/Access</li>
# <li>Cardiovascular Disease</li>
# <li>Housing</li>
# </ol>"),
# theme = value_box_theme(
# bg = if (input$mode_toggle %in% 'dark') chfs$cols9[2] else chfs$cols9[9],
# fg = if (input$mode_toggle %in% 'dark') 'white' else chfs$cols9[2]
# ),
# showcase = icon('calendar-check'),
# showcase_layout = "top right", full_screen = T, fill = T,
# height = NULL # Set height of the value box
# )
)
})
#
# Use leafletProxy() to update the map on reset instead of re-rendering it
observeEvent(input$resetMap, {
updateRadioButtons(session, 'labelthemap', selected = '12px')
update_switch('showmarkers', value = TRUE)
update_switch('showpopup', value = TRUE)
updateSelectInput(session, 'whichcounty', selected = 'All')
leafletProxy("map") %>%
realignViewOfKentucky()
# output$map <- renderLeaflet({
# mapPrecursor()
# })
})
# observeEvent(shapefileReactive(), {
# leafletProxy("map", data = shapefileReactive())
# })
#
observeEvent(input$resetdownloads, {
# Reset the selected input (e.g., set 'bydirectory' back to 'Allen County')
updateSelectInput(session, 'bydirectory', selected = 'Allen County')
# Clear the table by rendering a message or setting it to NULL
output$file_table <- renderTable({
data.frame(NULL)
})
})
# qpal --------------------------------------------------------------------
qpal <- reactive({
if (input$mode_toggle %in% 'dark') {
# dark
colorFactor(palette = c(chfs$cols9[2], chfs$cols9[7]), domain = shapefile$Status)
} else {
# light
colorFactor(palette = c(chfs$cols9[9], chfs$cols9[1]), domain = shapefile$Status)
}
})
shapefileReactive <- eventReactive(input$whichcounty, {
if (!input$whichcounty %in% 'All') {
shapefile %>%
mutate(whichrow = row_number()) %>%
# dplyr::filter(grepl(input$whichcounty, Listing))
# dplyr::filter(str_detect(Listing, fixed(input$whichcounty, ignore_case = TRUE)))
dplyr::filter(str_detect(Listing, paste0("(^|\\s)", input$whichcounty, "(\\s|$)")))
} else {
shapefile
}
}, ignoreInit = F)
# leaflet -----------------------------------------------------------------
mapPrecursor <- reactive({
leaflet(shapefileReactive()) %>%
addPolygons(,
fillColor = ~qpal()(Status),
fillOpacity = 1,
color = 'white',
weight = 2,
label = if (!input$showpopup) {NULL} else {
sprintf("%s",
paste('<span style="font-size: 1.5em">',
# "<b>Geography: </b>", shapefile$NAME10, '<br>',
'<b>LHD: </b>', shapefileReactive()$NAME10, '<br>',
'<b>Counties: </b>',shapefileReactive()$Listing, '<br>',
# "<b>Variable: </b>", a('link', href = 'kde.org', target='_blank'), '<br>',
"<b>LNA Submitted? </b>", shapefileReactive()$Status, '</span>'
)
) %>%
lapply(htmltools::HTML)}
) %>%
addControl(paste('902 KAR 8:160 Local health department operations requirements',br(), br(), 'Section 10: Identification of Local Needs'), position = 'topright') %>%
addLegend(title = HTML(paste0("<span style='color: #0C3151; font-size: 1.2em;'>", 'LNA Submitted?',"</span>")),
position = 'topright',
values = ~Status, # change here
pal = qpal(), # app WORKS
# pal = isolate(qpal()), # app WORKS
# pal = qpal, # interactive
opacity = 1
) %>%
{if (!input$showmarkers) {.} else {
addMarkers(.,
lng = ~ as.numeric(unlist(INTPTLON10)),
lat = ~ as.numeric(unlist(INTPTLAT10)),
popup = ~{
lapply(1:nrow(shapefileReactive()), function(i) {
marker_files <- nested_data_flat %>%
filter(NAME %in% shapefileReactive()$NAMELSAD10[i])
if (nrow(marker_files) == 0 || all(is.na(marker_files$files))) {
"No files available"
} else {
paste0("<ul class='mapfilebullet'>",
paste0("<li><a href='", marker_files$files,
"' target='_blank'>",
basename(marker_files$files),
"</a></li>", collapse = ""),
"</ul>")
}
})
})
}} %>%
# addMarkers(lng = centroid_coords[, 1], lat = centroid_coords[, 2]) %>%
{if (input$labelthemap %in% 'nolabels') {.} else {addLabelOnlyMarkers(.,
lng = ~ as.numeric(unlist(INTPTLON10)), lat = ~ as.numeric(unlist(INTPTLAT10)),
label = ~ NAMELSAD10,
icon = NULL,
labelOptions = labelOptions(
noHide = TRUE,
sticky=F,
textsize = input$labelthemap,
textOnly = T,
style = if (input$mode_toggle %in% 'light') {
list(
"color" = "black",
'text-shadow' = '0 0 10px #fff, 0 0 10px #fff, 0 0 10px #fff'
) }
else {
list(
"color" = "white",
'text-shadow' = '0 0 10px #95D3F5, 0 0 10px #95D3F5, 0 0 10px #95D3F5'
)
}
))}} %>%
leaflet.extras::setMapWidgetStyle(., if (input$mode_toggle %in% 'light') list(background = '#ddd') else list(background = 'black')) %>%
# {if (input$whichcounty %in% 'All') { realignViewOfKentucky(.) } else {setView(., lng = as.numeric(unlist(shapefileReactive()$INTPTLON10)), lat = as.numeric(unlist(shapefileReactive()$INTPTLAT10)), zoom = 9)}}
{if (input$whichcounty %in% 'All') { realignViewOfKentucky(.) } else {setView(., lng = centroid_coords[, 1][[shapefileReactive()$whichrow]], lat = centroid_coords[, 2][[shapefileReactive()$whichrow]], zoom = 10)}}
})
observeEvent(T, {
output$map <- renderLeaflet({
mapPrecursor()
})
}, once = T)
## Downloads panel
# Render the table with downloadable links
observeEvent(input$searchdownloads, {
selected_files <- reactive({
nested_data2 %>%
filter(NAME %in% isolate(input$bydirectory)) %>%
unnest(files) # Unnest the files for the selected directory
})
output$file_table <- renderTable({
# Check if selected_files() is empty
if (nrow(selected_files()) == 0) {
# Return a data frame with the "No files available" message
tibble("Downloadable Files" = paste("No files available for", isolate(input$bydirectory)))
} else {
# If there are files, generate the download links
selected_files() %>%
mutate(download_link = paste0("<a href='", files, "' download>", basename(files), "</a>")) %>%
select(download_link) %>%
rename("Downloadable Files" = download_link) # Display as clickable links
}
}, sanitize.text.function = function(x) x) # Disable sanitizing to allow HTML rendering
})
} # end Server