diego-ellis-soto
commited on
Commit
·
c8d7b4c
1
Parent(s):
d844214
Add shiny
Browse files- .DS_Store +0 -0
- R/.DS_Store +0 -0
- R/app.R +986 -0
- R/rsconnect/.DS_Store +0 -0
- R/rsconnect/shinyapps.io/diego-ellis-soto/RSF_Biodiversity_Access.dcf +12 -0
- www/California_academy_logo.png +0 -0
- www/Reimagining_San_Francisco.png +0 -0
- www/UC Berkeley_logo.png +0 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
R/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
R/app.R
ADDED
@@ -0,0 +1,986 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Sharing the app https://shiny.posit.co/r/getstarted/shiny-basics/lesson7/
|
2 |
+
# rsconnect::setAccountInfo(name='diego-ellis-soto', token='A47BE3C9E4B9EBCDFEC889AF31F64154', secret='g2Q2rxeYCiwlH81EkPXcCGsiHMgdyhTznJRmHtea')
|
3 |
+
# deployApp()
|
4 |
+
# Add that you can hover over the greespace and get its name
|
5 |
+
# Improve the titles of the ggplots of the model coefficient estimates and of ggplot using the gbif summary table on data avialability vs species richness. Also log transform these values for better data visualization
|
6 |
+
# Also the ggplot of data avialability vs species richness. should also update if the user decides to subset by class or family. Until then, its okay to retain the general plot using all the data from gbif_sf
|
7 |
+
|
8 |
+
# Optimize some calculations? Shorten
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
###############################################################################
|
15 |
+
# Shiny App: San Francisco Biodiversity Access Decision Support Tool
|
16 |
+
# Author: Diego Ellis Soto, et al.
|
17 |
+
# University of California Berkeley, ESPM
|
18 |
+
# California Academy of Sciences
|
19 |
+
###############################################################################
|
20 |
+
|
21 |
+
library(shiny)
|
22 |
+
library(leaflet)
|
23 |
+
library(mapboxapi)
|
24 |
+
library(tidyverse)
|
25 |
+
library(tidycensus)
|
26 |
+
library(sf)
|
27 |
+
library(DT)
|
28 |
+
library(RColorBrewer)
|
29 |
+
library(terra)
|
30 |
+
library(data.table) # for fread
|
31 |
+
library(mapview) # for mapview objects
|
32 |
+
library(sjPlot) # for plotting lm model coefficients
|
33 |
+
library(sjlabelled) # optional if needed for sjPlot
|
34 |
+
|
35 |
+
# ------------------------------------------------
|
36 |
+
# 1) API Keys
|
37 |
+
# ------------------------------------------------
|
38 |
+
mapbox_token <- "pk.eyJ1Ijoia3dhbGtlcnRjdSIsImEiOiJjbHc3NmI0cDMxYzhyMmt0OXBiYnltMjVtIn0.Thtu6WqIhOfin6AykskM2g"
|
39 |
+
mb_access_token(mapbox_token, install = FALSE)
|
40 |
+
|
41 |
+
# ------------------------------------------------
|
42 |
+
# 2) Load Data
|
43 |
+
# ------------------------------------------------
|
44 |
+
# -- Greenspace
|
45 |
+
osm_greenspace <- st_read("data/greenspaces_osm_nad83.shp", quiet = TRUE) %>%
|
46 |
+
st_transform(4326)
|
47 |
+
if (!"name" %in% names(osm_greenspace)) {
|
48 |
+
osm_greenspace$name <- "Unnamed Greenspace"
|
49 |
+
}
|
50 |
+
|
51 |
+
# -- NDVI Raster
|
52 |
+
ndvi <- rast("data/SF_EastBay_NDVI_Sentinel_10.tif")
|
53 |
+
|
54 |
+
# -- GBIF data
|
55 |
+
load("data/sf_gbif.Rdata") # => sf_gbif
|
56 |
+
|
57 |
+
# -- Precomputed CBG data
|
58 |
+
load('data/cbg_vect_sf.Rdata')
|
59 |
+
if (!"unique_species" %in% names(cbg_vect_sf)) {
|
60 |
+
cbg_vect_sf$unique_species <- cbg_vect_sf$n_species
|
61 |
+
}
|
62 |
+
if (!"n_observations" %in% names(cbg_vect_sf)) {
|
63 |
+
cbg_vect_sf$n_observations <- cbg_vect_sf$n
|
64 |
+
}
|
65 |
+
if (!"median_inc" %in% names(cbg_vect_sf)) {
|
66 |
+
cbg_vect_sf$median_inc <- cbg_vect_sf$medincE
|
67 |
+
}
|
68 |
+
if (!"ndvi_mean" %in% names(cbg_vect_sf)) {
|
69 |
+
cbg_vect_sf$ndvi_mean <- cbg_vect_sf$ndvi_sentinel
|
70 |
+
}
|
71 |
+
|
72 |
+
# -- Hotspots/Coldspots
|
73 |
+
biodiv_hotspots <- st_read("data/hotspots.shp", quiet = TRUE) %>% st_transform(4326)
|
74 |
+
biodiv_coldspots <- st_read("data/coldspots.shp", quiet = TRUE) %>% st_transform(4326)
|
75 |
+
|
76 |
+
# ------------------------------------------------
|
77 |
+
# 3) UI
|
78 |
+
# ------------------------------------------------
|
79 |
+
ui <- fluidPage(
|
80 |
+
titlePanel("San Francisco Biodiversity Access Decision Support Tool"),
|
81 |
+
|
82 |
+
fluidRow(
|
83 |
+
column(
|
84 |
+
width = 12, align = "center",
|
85 |
+
tags$img(src = "UC Berkeley_logo.png",
|
86 |
+
height = "120px", style = "margin:10px;"),
|
87 |
+
tags$img(src = "California_academy_logo.png",
|
88 |
+
height = "120px", style = "margin:10px;"),
|
89 |
+
tags$img(src = "Reimagining_San_Francisco.png",
|
90 |
+
height = "120px", style = "margin:10px;")
|
91 |
+
)
|
92 |
+
),
|
93 |
+
|
94 |
+
fluidRow(
|
95 |
+
column(
|
96 |
+
width = 12,
|
97 |
+
br(),
|
98 |
+
p("This application demonstrates an approach for exploring biodiversity access in San Francisco..."),
|
99 |
+
# (Your summary text can go here)
|
100 |
+
)
|
101 |
+
),
|
102 |
+
br(),
|
103 |
+
fluidRow(
|
104 |
+
column(
|
105 |
+
width = 12,
|
106 |
+
br(),
|
107 |
+
tags$b("App Summary (Fill out with RSF data working group):"),
|
108 |
+
# Increasingly, we ask ourselves about what increasing access to biodiversity really means.
|
109 |
+
# Importantly, accessibility differs from human mobility in urban planning studies for equitable transportation systems.
|
110 |
+
p("
|
111 |
+
This application allows users to either click on a map or geocode an address (in progress)
|
112 |
+
to generate travel-time isochrones across multiple transportation modes (e.g., pedestrian, cycling, driving, driving during traffic).
|
113 |
+
It retrieves socio-economic data from precomputed Census variables, calculates NDVI,
|
114 |
+
and summarizes biodiversity records from GBIF. We explore what biodiversity access means
|
115 |
+
Users can explore information that we often relate to biodiversity in urban environments including greenspace coverage, population estimates, and species diversity within each isochrone."),
|
116 |
+
|
117 |
+
tags$b("Reimagining San Francisco (Fill out with CAS):"),
|
118 |
+
p("Reimagining San Francisco is an initiative aimed at integrating ecological, social,
|
119 |
+
and technological dimensions to shape a sustainable future for the Bay Area.
|
120 |
+
This collaboration unites diverse stakeholders to explore innovations in urban planning,
|
121 |
+
conservation, and community engagement. The Reimagining San Francisco Data Working Group has been tasked with identifying and integrating multiple sources of socio-ecological biodiversity information in a co-development framework."),
|
122 |
+
|
123 |
+
tags$b("Why Biodiversity Access Matters (Polish this):"),
|
124 |
+
p("
|
125 |
+
# Ensuring equitable access to biodiversity is essential for human well-being,
|
126 |
+
# ecological resilience, and global policy decisions related to conservation.
|
127 |
+
# Areas with higher biodiversity can support ecosystem services including pollinators, moderate climate extremes,
|
128 |
+
# and provide cultural, recreational, and health benefits to local communities.
|
129 |
+
Recognizing that cities are particularly complex socio-ecological systems facing both legacies of sociocultural practices as well as current ongoing dynamic human activities and pressures.
|
130 |
+
Incorporating multiple facets of biodiversity metrics alongside variables employed by city planners, human geographers, and decision-makers into urban planning will allow a more integrative lens in creating a sustainable future for cities and their residents."),
|
131 |
+
|
132 |
+
tags$b("How We Calculate Biodiversity Access Percentile:"),
|
133 |
+
p("Total unique species found within the user-generated isochrone.
|
134 |
+
We then compare that value to the distribution of unique species counts across all census block groups,
|
135 |
+
converting that comparison into a percentile ranking (Polish this, look at the 15 Minute city).
|
136 |
+
A higher percentile indicates greater biodiversity within the chosen area,
|
137 |
+
relative to other parts of the city or region."),
|
138 |
+
|
139 |
+
tags$b("Created by:"),
|
140 |
+
p(strong("Diego Ellis Soto", "Carl Boettiger, Rebecca Johnson, Christopher J. Schell")),
|
141 |
+
|
142 |
+
p("Contact Information",
|
143 |
+
strong("[email protected]")),
|
144 |
+
|
145 |
+
tags$b("Next Steps:"),
|
146 |
+
tags$ul(
|
147 |
+
tags$li("Add impervious surface"),
|
148 |
+
tags$li("National walkability score"),
|
149 |
+
tags$li("Social vulnerability score"),
|
150 |
+
tags$li("NatureServe biodiversity maps"),
|
151 |
+
tags$li("Calculate cold-hotspots within ggregation of H6 bins instead of by census block group: Ask Carl"),
|
152 |
+
tags$li("Species range maps"),
|
153 |
+
tags$li("Add common name GBIF"),
|
154 |
+
tags$li("Partner orgs"),
|
155 |
+
tags$li("Optimize speed -> store variables -> H-ify the world?"),
|
156 |
+
tags$li("Brainstorm and co-develop the biodiversity access score"),
|
157 |
+
tags$li("For the GBIF summaries, add an annotated GBIF_sf with environmental variables so we can see landcover type association across the biodiversity within the isochrone.")
|
158 |
+
)
|
159 |
+
)
|
160 |
+
),
|
161 |
+
br(),
|
162 |
+
|
163 |
+
tabsetPanel(
|
164 |
+
|
165 |
+
# 1) Isochrone Explorer
|
166 |
+
tabPanel("Isochrone Explorer",
|
167 |
+
sidebarLayout(
|
168 |
+
sidebarPanel(
|
169 |
+
radioButtons(
|
170 |
+
"location_choice",
|
171 |
+
"Select how to choose your location:",
|
172 |
+
choices = c("Address (Geocode)" = "address",
|
173 |
+
"Click on Map" = "map_click"),
|
174 |
+
selected = "map_click"
|
175 |
+
),
|
176 |
+
|
177 |
+
conditionalPanel(
|
178 |
+
condition = "input.location_choice == 'address'",
|
179 |
+
textInput(
|
180 |
+
"user_address",
|
181 |
+
"Enter Address:",
|
182 |
+
value = "",
|
183 |
+
placeholder = "e.g., 1600 Amphitheatre Parkway, Mountain View, CA"
|
184 |
+
)
|
185 |
+
),
|
186 |
+
|
187 |
+
checkboxGroupInput(
|
188 |
+
"transport_modes",
|
189 |
+
"Select Transportation Modes:",
|
190 |
+
choices = list("Driving" = "driving",
|
191 |
+
"Walking" = "walking",
|
192 |
+
"Cycling" = "cycling",
|
193 |
+
"Driving with Traffic"= "driving-traffic"),
|
194 |
+
selected = c("driving", "walking")
|
195 |
+
),
|
196 |
+
|
197 |
+
checkboxGroupInput(
|
198 |
+
"iso_times",
|
199 |
+
"Select Isochrone Times (minutes):",
|
200 |
+
choices = list("5" = 5, "10" = 10, "15" = 15),
|
201 |
+
selected = c(5, 10)
|
202 |
+
),
|
203 |
+
|
204 |
+
actionButton("generate_iso", "Generate Isochrones"),
|
205 |
+
actionButton("clear_map", "Clear")
|
206 |
+
|
207 |
+
),
|
208 |
+
|
209 |
+
mainPanel(
|
210 |
+
leafletOutput("isoMap", height = 600),
|
211 |
+
|
212 |
+
fluidRow(
|
213 |
+
column(12,
|
214 |
+
br(),
|
215 |
+
uiOutput("bioScoreBox"),
|
216 |
+
uiOutput("closestGreenspaceUI")
|
217 |
+
)
|
218 |
+
),
|
219 |
+
|
220 |
+
br(),
|
221 |
+
DTOutput("dataTable"),
|
222 |
+
|
223 |
+
br(),
|
224 |
+
fluidRow(
|
225 |
+
column(12,
|
226 |
+
plotOutput("bioSocPlot", height = "400px")
|
227 |
+
)
|
228 |
+
),
|
229 |
+
|
230 |
+
br(),
|
231 |
+
fluidRow(
|
232 |
+
column(12,
|
233 |
+
plotOutput("collectionPlot", height = "300px")
|
234 |
+
)
|
235 |
+
)
|
236 |
+
)
|
237 |
+
)
|
238 |
+
),
|
239 |
+
|
240 |
+
#br.?
|
241 |
+
tabPanel(
|
242 |
+
"GBIF Summaries",
|
243 |
+
sidebarLayout(
|
244 |
+
sidebarPanel(
|
245 |
+
selectInput(
|
246 |
+
"class_filter",
|
247 |
+
"Select a GBIF Class to Summarize:",
|
248 |
+
choices = c("All", sort(unique(sf_gbif$class))),
|
249 |
+
selected = "All"
|
250 |
+
),
|
251 |
+
selectInput(
|
252 |
+
"family_filter",
|
253 |
+
"Filter by Family (optional):",
|
254 |
+
choices = c("All", sort(unique(sf_gbif$family))),
|
255 |
+
selected = "All"
|
256 |
+
)
|
257 |
+
),
|
258 |
+
mainPanel(
|
259 |
+
DTOutput("classTable"),
|
260 |
+
br(),
|
261 |
+
h3("Observations vs. Species Richness"),
|
262 |
+
plotOutput("obsVsSpeciesPlot", height = "400px"),
|
263 |
+
p("This plot displays the relationship between the number of observations and the species richness. Use this visualization to understand data coverage and biodiversity trends.")
|
264 |
+
)
|
265 |
+
)
|
266 |
+
)
|
267 |
+
|
268 |
+
|
269 |
+
# )
|
270 |
+
|
271 |
+
# Separate section for the plot outside of the "GBIF Summaries" tab
|
272 |
+
|
273 |
+
# tabsetPanel(
|
274 |
+
|
275 |
+
# # 1) Isochrone Explorer
|
276 |
+
# tabPanel(
|
277 |
+
# mainPanel(
|
278 |
+
# DTOutput("classTable"),
|
279 |
+
# br(),
|
280 |
+
# fluidRow(
|
281 |
+
# column(
|
282 |
+
# 6,
|
283 |
+
# # A simple scatter or line plot for n_observations vs n_species
|
284 |
+
# plotOutput("obsVsSpeciesPlot", height = "300px")
|
285 |
+
# )
|
286 |
+
# # ,
|
287 |
+
# # column(
|
288 |
+
# # 6,
|
289 |
+
# # # A regression model plot using sjPlot
|
290 |
+
# # plotOutput("lmCoefficientsPlot", height = "300px")
|
291 |
+
# # )
|
292 |
+
# )
|
293 |
+
# )
|
294 |
+
# )
|
295 |
+
# ),
|
296 |
+
#
|
297 |
+
# br()
|
298 |
+
|
299 |
+
)
|
300 |
+
|
301 |
+
|
302 |
+
# fluidRow(
|
303 |
+
# column(
|
304 |
+
# 12,
|
305 |
+
# tags$h3("Species Richness vs Data Availability"),
|
306 |
+
# fluidRow(
|
307 |
+
# column(6, uiOutput("mapNUI")),
|
308 |
+
# column(6, uiOutput("mapSpeciesUI"))
|
309 |
+
# )
|
310 |
+
# )
|
311 |
+
# )
|
312 |
+
)
|
313 |
+
|
314 |
+
# ------------------------------------------------
|
315 |
+
# 4) Server
|
316 |
+
# ------------------------------------------------
|
317 |
+
server <- function(input, output, session) {
|
318 |
+
|
319 |
+
chosen_point <- reactiveVal(NULL)
|
320 |
+
|
321 |
+
# ------------------------------------------------
|
322 |
+
# Leaflet Base + Hide Overlays
|
323 |
+
# ------------------------------------------------
|
324 |
+
output$isoMap <- renderLeaflet({
|
325 |
+
pal_cbg <- colorNumeric("YlOrRd", cbg_vect_sf$medincE)
|
326 |
+
|
327 |
+
pal_rich <- colorNumeric("YlOrRd", domain = cbg_vect_sf$unique_species)
|
328 |
+
# 2) Color palette for data availability
|
329 |
+
pal_data <- colorNumeric("Blues", domain = cbg_vect_sf$n_observations)
|
330 |
+
|
331 |
+
|
332 |
+
leaflet() %>%
|
333 |
+
addTiles(group = "Street Map (Default)") %>%
|
334 |
+
addProviderTiles(providers$Esri.WorldImagery, group = "Satellite (ESRI)") %>%
|
335 |
+
addProviderTiles(providers$CartoDB.Positron, group = "CartoDB.Positron") %>%
|
336 |
+
|
337 |
+
addPolygons(
|
338 |
+
data = cbg_vect_sf,
|
339 |
+
group = "Income",
|
340 |
+
# fillColor = ~pal_cbg(unique_species),
|
341 |
+
fillColor = ~pal_cbg(medincE),
|
342 |
+
fillOpacity = 0.6,
|
343 |
+
color = "white",
|
344 |
+
weight = 1,
|
345 |
+
label = "Income"
|
346 |
+
) %>%
|
347 |
+
|
348 |
+
addPolygons(
|
349 |
+
data = osm_greenspace,
|
350 |
+
group = "Greenspace",
|
351 |
+
fillColor = "darkgreen",
|
352 |
+
fillOpacity = 0.3,
|
353 |
+
color = "green",
|
354 |
+
weight = 1,
|
355 |
+
label = ~name,
|
356 |
+
highlightOptions = highlightOptions(
|
357 |
+
weight = 5,
|
358 |
+
color = "blue",
|
359 |
+
fillOpacity = 0.5,
|
360 |
+
bringToFront = TRUE
|
361 |
+
),
|
362 |
+
labelOptions = labelOptions(
|
363 |
+
style = list("font-weight" = "bold", "color" = "blue"),
|
364 |
+
textsize = "12px",
|
365 |
+
direction = "auto"
|
366 |
+
)
|
367 |
+
) %>%
|
368 |
+
|
369 |
+
addPolygons(
|
370 |
+
data = biodiv_hotspots,
|
371 |
+
group = "Hotspots (KnowBR)",
|
372 |
+
fillColor = "firebrick",
|
373 |
+
fillOpacity = 0.2,
|
374 |
+
color = "firebrick",
|
375 |
+
weight = 2,
|
376 |
+
label = "Biodiversity Hotspot"
|
377 |
+
) %>%
|
378 |
+
|
379 |
+
addPolygons(
|
380 |
+
data = biodiv_coldspots,
|
381 |
+
group = "Coldspots (KnowBR)",
|
382 |
+
fillColor = "navyblue",
|
383 |
+
fillOpacity = 0.2,
|
384 |
+
color = "navyblue",
|
385 |
+
weight = 2,
|
386 |
+
label = "Biodiversity Coldspot"
|
387 |
+
) %>%
|
388 |
+
|
389 |
+
# Add richness and nobs
|
390 |
+
# -- Richness layer
|
391 |
+
addPolygons(
|
392 |
+
data = cbg_vect_sf,
|
393 |
+
group = "Species Richness",
|
394 |
+
fillColor = ~pal_rich(unique_species),
|
395 |
+
fillOpacity = 0.6,
|
396 |
+
color = "white",
|
397 |
+
weight = 1,
|
398 |
+
popup = ~paste0(
|
399 |
+
"<strong>GEOID: </strong>", GEOID,
|
400 |
+
"<br><strong>Species Richness: </strong>", unique_species,
|
401 |
+
"<br><strong>Observations: </strong>", n_observations,
|
402 |
+
"<br><strong>Median Income: </strong>", median_inc,
|
403 |
+
"<br><strong>Mean NDVI: </strong>", ndvi_mean
|
404 |
+
)
|
405 |
+
) %>%
|
406 |
+
|
407 |
+
# -- Data Availability layer
|
408 |
+
addPolygons(
|
409 |
+
data = cbg_vect_sf,
|
410 |
+
group = "Data Availability",
|
411 |
+
fillColor = ~pal_data(n_observations),
|
412 |
+
fillOpacity = 0.6,
|
413 |
+
color = "white",
|
414 |
+
weight = 1,
|
415 |
+
popup = ~paste0(
|
416 |
+
"<strong>GEOID: </strong>", GEOID,
|
417 |
+
"<br><strong>Observations: </strong>", n_observations,
|
418 |
+
"<br><strong>Species Richness: </strong>", unique_species,
|
419 |
+
"<br><strong>Median Income: </strong>", median_inc,
|
420 |
+
"<br><strong>Mean NDVI: </strong>", ndvi_mean
|
421 |
+
)
|
422 |
+
) %>%
|
423 |
+
|
424 |
+
|
425 |
+
setView(lng = -122.4194, lat = 37.7749, zoom = 12) %>%
|
426 |
+
addLayersControl(
|
427 |
+
baseGroups = c("Street Map (Default)", "Satellite (ESRI)", "CartoDB.Positron"),
|
428 |
+
overlayGroups = c("Income", "Greenspace","Species Richness", "Data Availability",
|
429 |
+
"Hotspots (KnowBR)", "Coldspots (KnowBR)"),
|
430 |
+
options = layersControlOptions(collapsed = FALSE)
|
431 |
+
) %>%
|
432 |
+
hideGroup("Income") %>%
|
433 |
+
hideGroup("Greenspace") %>%
|
434 |
+
hideGroup("Hotspots (KnowBR)") %>%
|
435 |
+
hideGroup("Coldspots (KnowBR)") %>%
|
436 |
+
hideGroup("Species Richness") %>%
|
437 |
+
hideGroup("Data Availability")
|
438 |
+
})
|
439 |
+
|
440 |
+
|
441 |
+
# ------------------------------------------------
|
442 |
+
# Observe map clicks (location_choice = 'map_click')
|
443 |
+
# ------------------------------------------------
|
444 |
+
observeEvent(input$isoMap_click, {
|
445 |
+
req(input$location_choice == "map_click")
|
446 |
+
click <- input$isoMap_click
|
447 |
+
if (!is.null(click)) {
|
448 |
+
chosen_point(c(lon = click$lng, lat = click$lat))
|
449 |
+
leafletProxy("isoMap") %>%
|
450 |
+
clearMarkers() %>%
|
451 |
+
addCircleMarkers(
|
452 |
+
lng = click$lng, lat = click$lat,
|
453 |
+
radius = 6, color = "firebrick",
|
454 |
+
label = "Map Click Location"
|
455 |
+
)
|
456 |
+
}
|
457 |
+
})
|
458 |
+
|
459 |
+
# ------------------------------------------------
|
460 |
+
# Observe clearinf of map
|
461 |
+
# ------------------------------------------------
|
462 |
+
observeEvent(input$clear_map, {
|
463 |
+
# Reset the chosen point
|
464 |
+
chosen_point(NULL)
|
465 |
+
|
466 |
+
# Clear all markers and isochrones from the map
|
467 |
+
leafletProxy("isoMap") %>%
|
468 |
+
clearMarkers() %>%
|
469 |
+
clearShapes() %>%
|
470 |
+
clearGroup("Isochrones") %>%
|
471 |
+
clearGroup("NDVI Raster")
|
472 |
+
|
473 |
+
# Optional: Reset any other reactive values if needed
|
474 |
+
showNotification("Map cleared. You can select a new location.")
|
475 |
+
})
|
476 |
+
|
477 |
+
# ------------------------------------------------
|
478 |
+
# Generate Isochrones
|
479 |
+
# ------------------------------------------------
|
480 |
+
isochrones_data <- eventReactive(input$generate_iso, {
|
481 |
+
|
482 |
+
leafletProxy("isoMap") %>%
|
483 |
+
clearGroup("Isochrones") %>%
|
484 |
+
clearGroup("NDVI Raster")
|
485 |
+
|
486 |
+
# If user selected address:
|
487 |
+
if (input$location_choice == "address") {
|
488 |
+
if (nchar(input$user_address) < 5) {
|
489 |
+
showNotification("Please enter a more complete address.", type = "error")
|
490 |
+
return(NULL)
|
491 |
+
}
|
492 |
+
|
493 |
+
loc_df <- tryCatch({
|
494 |
+
mb_geocode(input$user_address, access_token = mapbox_token)
|
495 |
+
}, error = function(e) {
|
496 |
+
showNotification(paste("Geocoding failed:", e$message), type = "error")
|
497 |
+
NULL
|
498 |
+
})
|
499 |
+
|
500 |
+
# Check for valid lat/lon
|
501 |
+
if (is.null(loc_df) || nrow(loc_df) == 0 || is.na(loc_df$lon[1]) || is.na(loc_df$lat[1])) {
|
502 |
+
showNotification("No valid geocoding results found.", type = "warning")
|
503 |
+
return(NULL)
|
504 |
+
}
|
505 |
+
|
506 |
+
chosen_point(c(lon = loc_df$lon[1], lat = loc_df$lat[1]))
|
507 |
+
|
508 |
+
leafletProxy("isoMap") %>%
|
509 |
+
clearMarkers() %>%
|
510 |
+
addCircleMarkers(
|
511 |
+
lng = loc_df$lon[1], lat = loc_df$lat[1],
|
512 |
+
radius = 6, color = "navyblue",
|
513 |
+
label = "Geocoded Address"
|
514 |
+
) %>%
|
515 |
+
setView(lng = loc_df$lon[1], lat = loc_df$lat[1], zoom = 13)
|
516 |
+
}
|
517 |
+
|
518 |
+
pt <- chosen_point()
|
519 |
+
if (is.null(pt)) {
|
520 |
+
showNotification("No location selected! Provide an address or click the map.", type = "error")
|
521 |
+
return(NULL)
|
522 |
+
}
|
523 |
+
if (length(input$transport_modes) == 0) {
|
524 |
+
showNotification("Select at least one transportation mode.", type = "error")
|
525 |
+
return(NULL)
|
526 |
+
}
|
527 |
+
if (length(input$iso_times) == 0) {
|
528 |
+
showNotification("Select at least one isochrone time.", type = "error")
|
529 |
+
return(NULL)
|
530 |
+
}
|
531 |
+
|
532 |
+
location_sf <- st_as_sf(
|
533 |
+
data.frame(lon = pt["lon"], lat = pt["lat"]),
|
534 |
+
coords = c("lon","lat"), crs = 4326
|
535 |
+
)
|
536 |
+
|
537 |
+
iso_list <- list()
|
538 |
+
for (mode in input$transport_modes) {
|
539 |
+
for (t in input$iso_times) {
|
540 |
+
iso <- tryCatch({
|
541 |
+
mb_isochrone(location_sf, time = as.numeric(t), profile = mode,
|
542 |
+
access_token = mapbox_token)
|
543 |
+
}, error = function(e) {
|
544 |
+
showNotification(paste("Isochrone error:", mode, t, e$message), type = "error")
|
545 |
+
NULL
|
546 |
+
})
|
547 |
+
if (!is.null(iso)) {
|
548 |
+
iso$mode <- mode
|
549 |
+
iso$time <- t
|
550 |
+
iso_list <- append(iso_list, list(iso))
|
551 |
+
}
|
552 |
+
}
|
553 |
+
}
|
554 |
+
if (length(iso_list) == 0) {
|
555 |
+
showNotification("No isochrones generated.", type = "warning")
|
556 |
+
return(NULL)
|
557 |
+
}
|
558 |
+
|
559 |
+
all_iso <- do.call(rbind, iso_list) %>% st_transform(4326)
|
560 |
+
all_iso
|
561 |
+
})
|
562 |
+
|
563 |
+
# ------------------------------------------------
|
564 |
+
# Plot Isochrones + NDVI
|
565 |
+
# ------------------------------------------------
|
566 |
+
observeEvent(isochrones_data(), {
|
567 |
+
iso_data <- isochrones_data()
|
568 |
+
req(iso_data)
|
569 |
+
|
570 |
+
iso_data$iso_group <- paste(iso_data$mode, iso_data$time, sep = "_")
|
571 |
+
pal <- colorRampPalette(brewer.pal(8, "Set2"))
|
572 |
+
cols <- pal(nrow(iso_data))
|
573 |
+
|
574 |
+
for (i in seq_len(nrow(iso_data))) {
|
575 |
+
poly_i <- iso_data[i, ]
|
576 |
+
leafletProxy("isoMap") %>%
|
577 |
+
addPolygons(
|
578 |
+
data = poly_i,
|
579 |
+
group = "Isochrones",
|
580 |
+
color = cols[i],
|
581 |
+
weight = 2,
|
582 |
+
fillOpacity = 0.4,
|
583 |
+
label = paste0(poly_i$mode, " - ", poly_i$time, " mins")
|
584 |
+
)
|
585 |
+
}
|
586 |
+
|
587 |
+
iso_union <- st_union(iso_data)
|
588 |
+
iso_union_vect <- vect(iso_union)
|
589 |
+
ndvi_crop <- crop(ndvi, iso_union_vect)
|
590 |
+
ndvi_mask <- mask(ndvi_crop, iso_union_vect)
|
591 |
+
ndvi_vals <- values(ndvi_mask)
|
592 |
+
ndvi_vals <- ndvi_vals[!is.na(ndvi_vals)]
|
593 |
+
|
594 |
+
if (length(ndvi_vals) > 0) {
|
595 |
+
ndvi_pal <- colorNumeric("YlGn", domain = range(ndvi_vals, na.rm = TRUE), na.color = "transparent")
|
596 |
+
|
597 |
+
leafletProxy("isoMap") %>%
|
598 |
+
addRasterImage(
|
599 |
+
x = ndvi_mask,
|
600 |
+
colors = ndvi_pal,
|
601 |
+
opacity = 0.7,
|
602 |
+
project = TRUE,
|
603 |
+
group = "NDVI Raster"
|
604 |
+
) %>%
|
605 |
+
addLegend(
|
606 |
+
position = "bottomright",
|
607 |
+
pal = ndvi_pal,
|
608 |
+
values = ndvi_vals,
|
609 |
+
title = "NDVI"
|
610 |
+
)
|
611 |
+
}
|
612 |
+
|
613 |
+
leafletProxy("isoMap") %>%
|
614 |
+
addLayersControl(
|
615 |
+
baseGroups = c("Street Map (Default)", "Satellite (ESRI)", "CartoDB.Positron"),
|
616 |
+
overlayGroups = c("Income", "Greenspace",
|
617 |
+
"Hotspots (KnowBR)", "Coldspots (KnowBR)",
|
618 |
+
"Isochrones", "NDVI Raster"),
|
619 |
+
options = layersControlOptions(collapsed = FALSE)
|
620 |
+
)
|
621 |
+
})
|
622 |
+
|
623 |
+
# ------------------------------------------------
|
624 |
+
# socio_data Reactive + Summaries
|
625 |
+
# ------------------------------------------------
|
626 |
+
socio_data <- reactive({
|
627 |
+
iso_data <- isochrones_data()
|
628 |
+
if (is.null(iso_data) || nrow(iso_data) == 0) {
|
629 |
+
return(data.frame())
|
630 |
+
}
|
631 |
+
|
632 |
+
acs_wide <- cbg_vect_sf %>%
|
633 |
+
mutate(
|
634 |
+
population = popE,
|
635 |
+
med_income = medincE
|
636 |
+
)
|
637 |
+
|
638 |
+
hotspot_union <- st_union(biodiv_hotspots)
|
639 |
+
coldspot_union <- st_union(biodiv_coldspots)
|
640 |
+
|
641 |
+
results <- data.frame()
|
642 |
+
|
643 |
+
for (i in seq_len(nrow(iso_data))) {
|
644 |
+
poly_i <- iso_data[i, ]
|
645 |
+
|
646 |
+
dist_hot <- st_distance(poly_i, hotspot_union)
|
647 |
+
dist_cold <- st_distance(poly_i, coldspot_union)
|
648 |
+
dist_hot_km <- round(as.numeric(min(dist_hot)) / 1000, 3)
|
649 |
+
dist_cold_km <- round(as.numeric(min(dist_cold)) / 1000, 3)
|
650 |
+
|
651 |
+
inter_acs <- st_intersection(acs_wide, poly_i)
|
652 |
+
|
653 |
+
pop_total <- 0
|
654 |
+
inc_str <- "N/A"
|
655 |
+
if (nrow(inter_acs) > 0) {
|
656 |
+
inter_acs$area <- st_area(inter_acs)
|
657 |
+
inter_acs$area_num <- as.numeric(inter_acs$area)
|
658 |
+
inter_acs$area_ratio <- inter_acs$area_num / as.numeric(st_area(inter_acs))
|
659 |
+
inter_acs$weighted_pop <- inter_acs$population * inter_acs$area_ratio
|
660 |
+
|
661 |
+
pop_total <- round(sum(inter_acs$weighted_pop, na.rm = TRUE))
|
662 |
+
|
663 |
+
w_income <- sum(inter_acs$med_income * inter_acs$area_num, na.rm = TRUE) /
|
664 |
+
sum(inter_acs$area_num, na.rm = TRUE)
|
665 |
+
if (!is.na(w_income) && w_income > 0) {
|
666 |
+
inc_str <- paste0("$", formatC(round(w_income, 2), format = "f", big.mark = ","))
|
667 |
+
}
|
668 |
+
}
|
669 |
+
|
670 |
+
inter_gs <- st_intersection(osm_greenspace, poly_i)
|
671 |
+
gs_area_m2 <- 0
|
672 |
+
if (nrow(inter_gs) > 0) {
|
673 |
+
gs_area_m2 <- sum(st_area(inter_gs))
|
674 |
+
}
|
675 |
+
iso_area_m2 <- as.numeric(st_area(poly_i))
|
676 |
+
gs_area_m2 <- as.numeric(gs_area_m2)
|
677 |
+
gs_percent <- ifelse(iso_area_m2 > 0, 100 * gs_area_m2 / iso_area_m2, 0)
|
678 |
+
|
679 |
+
poly_vect <- vect(poly_i)
|
680 |
+
ndvi_crop <- crop(ndvi, poly_vect)
|
681 |
+
ndvi_mask <- mask(ndvi_crop, poly_vect)
|
682 |
+
ndvi_vals <- values(ndvi_mask)
|
683 |
+
ndvi_vals <- ndvi_vals[!is.na(ndvi_vals)]
|
684 |
+
mean_ndvi <- ifelse(length(ndvi_vals) > 0, round(mean(ndvi_vals, na.rm=TRUE), 3), NA)
|
685 |
+
|
686 |
+
inter_gbif <- st_intersection(sf_gbif, poly_i)
|
687 |
+
n_records <- nrow(inter_gbif)
|
688 |
+
n_species <- length(unique(inter_gbif$species))
|
689 |
+
|
690 |
+
n_birds <- length(unique(inter_gbif$species[ inter_gbif$class == "Aves" ]))
|
691 |
+
n_mammals <- length(unique(inter_gbif$species[ inter_gbif$class == "Mammalia" ]))
|
692 |
+
n_plants <- length(unique(inter_gbif$species[ inter_gbif$class %in%
|
693 |
+
c("Magnoliopsida","Liliopsida","Pinopsida","Polypodiopsida",
|
694 |
+
"Equisetopsida","Bryopsida","Marchantiopsida") ]))
|
695 |
+
|
696 |
+
iso_area_km2 <- round(iso_area_m2 / 1e6, 3)
|
697 |
+
iso_area_sqm <- round(iso_area_m2, 2)
|
698 |
+
|
699 |
+
row_i <- data.frame(
|
700 |
+
Mode = tools::toTitleCase(poly_i$mode),
|
701 |
+
Time = poly_i$time,
|
702 |
+
IsochroneArea_m2 = iso_area_sqm,
|
703 |
+
IsochroneArea_km2 = iso_area_km2,
|
704 |
+
DistToHotspot_km = dist_hot_km,
|
705 |
+
DistToColdspot_km = dist_cold_km,
|
706 |
+
EstimatedPopulation = pop_total,
|
707 |
+
MedianIncome = inc_str,
|
708 |
+
MeanNDVI = ifelse(!is.na(mean_ndvi), mean_ndvi, "N/A"),
|
709 |
+
GBIF_Records = n_records,
|
710 |
+
GBIF_Species = n_species,
|
711 |
+
Bird_Species = n_birds,
|
712 |
+
Mammal_Species = n_mammals,
|
713 |
+
Plant_Species = n_plants,
|
714 |
+
Greenspace_m2 = round(gs_area_m2, 2),
|
715 |
+
Greenspace_percent = round(gs_percent, 2),
|
716 |
+
stringsAsFactors = FALSE
|
717 |
+
)
|
718 |
+
results <- rbind(results, row_i)
|
719 |
+
}
|
720 |
+
|
721 |
+
iso_union <- st_union(iso_data)
|
722 |
+
inter_all_gbif <- st_intersection(sf_gbif, iso_union)
|
723 |
+
union_n_species <- length(unique(inter_all_gbif$species))
|
724 |
+
rank_percentile <- round(100 * ecdf(cbg_vect_sf$unique_species)(union_n_species), 1)
|
725 |
+
attr(results, "bio_percentile") <- rank_percentile
|
726 |
+
|
727 |
+
# Closest Greenspace from ANY part of the isochrone
|
728 |
+
dist_mat <- st_distance(iso_union, osm_greenspace) # 1 x N matrix
|
729 |
+
if (length(dist_mat) > 0) {
|
730 |
+
min_dist <- min(dist_mat)
|
731 |
+
min_idx <- which.min(dist_mat)
|
732 |
+
gs_name <- osm_greenspace$name[min_idx]
|
733 |
+
attr(results, "closest_greenspace") <- gs_name
|
734 |
+
} else {
|
735 |
+
attr(results, "closest_greenspace") <- "None"
|
736 |
+
}
|
737 |
+
|
738 |
+
results
|
739 |
+
})
|
740 |
+
|
741 |
+
# ------------------------------------------------
|
742 |
+
# Render main summary table
|
743 |
+
# ------------------------------------------------
|
744 |
+
output$dataTable <- renderDT({
|
745 |
+
df <- socio_data()
|
746 |
+
if (nrow(df) == 0) {
|
747 |
+
return(DT::datatable(data.frame("Message" = "No isochrones generated yet.")))
|
748 |
+
}
|
749 |
+
DT::datatable(
|
750 |
+
df,
|
751 |
+
colnames = c(
|
752 |
+
"Mode" = "Mode",
|
753 |
+
"Time (min)" = "Time",
|
754 |
+
"Area (m²)" = "IsochroneArea_m2",
|
755 |
+
"Area (km²)" = "IsochroneArea_km2",
|
756 |
+
"Dist. Hotspot (km)" = "DistToHotspot_km",
|
757 |
+
"Dist. Coldspot (km)" = "DistToColdspot_km",
|
758 |
+
"Population" = "EstimatedPopulation",
|
759 |
+
"Median Income" = "MedianIncome",
|
760 |
+
"Mean NDVI" = "MeanNDVI",
|
761 |
+
"GBIF Records" = "GBIF_Records",
|
762 |
+
"Unique Species" = "GBIF_Species",
|
763 |
+
"Bird Species" = "Bird_Species",
|
764 |
+
"Mammal Species" = "Mammal_Species",
|
765 |
+
"Plant Species" = "Plant_Species",
|
766 |
+
"Greenspace (m²)" = "Greenspace_m2",
|
767 |
+
"Greenspace (%)" = "Greenspace_percent"
|
768 |
+
),
|
769 |
+
options = list(pageLength = 10, autoWidth = TRUE),
|
770 |
+
rownames = FALSE
|
771 |
+
)
|
772 |
+
})
|
773 |
+
|
774 |
+
# ------------------------------------------------
|
775 |
+
# Biodiversity Access Score + Closest Greenspace
|
776 |
+
# ------------------------------------------------
|
777 |
+
output$bioScoreBox <- renderUI({
|
778 |
+
df <- socio_data()
|
779 |
+
if (nrow(df) == 0) return(NULL)
|
780 |
+
|
781 |
+
percentile <- attr(df, "bio_percentile")
|
782 |
+
if (is.null(percentile)) percentile <- "N/A"
|
783 |
+
else percentile <- paste0(percentile, "th Percentile")
|
784 |
+
|
785 |
+
wellPanel(
|
786 |
+
HTML(paste0("<h2>Biodiversity Access Score: ", percentile, "</h2>"))
|
787 |
+
)
|
788 |
+
})
|
789 |
+
|
790 |
+
output$closestGreenspaceUI <- renderUI({
|
791 |
+
df <- socio_data()
|
792 |
+
if (nrow(df) == 0) return(NULL)
|
793 |
+
gs_name <- attr(df, "closest_greenspace")
|
794 |
+
if (is.null(gs_name)) gs_name <- "None"
|
795 |
+
|
796 |
+
tagList(
|
797 |
+
strong("Closest Greenspace (from any part of the Isochrone):"),
|
798 |
+
p(gs_name)
|
799 |
+
)
|
800 |
+
})
|
801 |
+
|
802 |
+
# ------------------------------------------------
|
803 |
+
# Secondary table: user-selected CLASS & FAMILY
|
804 |
+
# ------------------------------------------------
|
805 |
+
output$classTable <- renderDT({
|
806 |
+
iso_data <- isochrones_data()
|
807 |
+
if (is.null(iso_data) || nrow(iso_data) == 0) {
|
808 |
+
return(DT::datatable(data.frame("Message" = "No isochrones generated yet.")))
|
809 |
+
}
|
810 |
+
|
811 |
+
iso_union <- st_union(iso_data)
|
812 |
+
inter_gbif <- st_intersection(sf_gbif, iso_union)
|
813 |
+
|
814 |
+
# Add a quick ACS intersection for mean income & NDVI if needed
|
815 |
+
acs_wide <- cbg_vect_sf %>% mutate(
|
816 |
+
income = median_inc,
|
817 |
+
ndvi = ndvi_mean
|
818 |
+
)
|
819 |
+
|
820 |
+
inter_gbif_acs <- st_intersection(inter_gbif, acs_wide)
|
821 |
+
|
822 |
+
if (input$class_filter != "All") {
|
823 |
+
inter_gbif_acs <- inter_gbif_acs[ inter_gbif_acs$class == input$class_filter, ]
|
824 |
+
}
|
825 |
+
if (input$family_filter != "All") {
|
826 |
+
inter_gbif_acs <- inter_gbif_acs[ inter_gbif_acs$family == input$family_filter, ]
|
827 |
+
}
|
828 |
+
|
829 |
+
if (nrow(inter_gbif_acs) == 0) {
|
830 |
+
return(DT::datatable(data.frame("Message" = "No records for that combination in the isochrone.")))
|
831 |
+
}
|
832 |
+
|
833 |
+
species_counts <- inter_gbif_acs %>%
|
834 |
+
st_drop_geometry() %>%
|
835 |
+
group_by(species) %>%
|
836 |
+
summarize(
|
837 |
+
n_records = n(),
|
838 |
+
mean_income = round(mean(income, na.rm=TRUE), 2),
|
839 |
+
mean_ndvi = round(mean(ndvi, na.rm=TRUE), 3),
|
840 |
+
.groups = "drop"
|
841 |
+
) %>%
|
842 |
+
arrange(desc(n_records))
|
843 |
+
|
844 |
+
DT::datatable(
|
845 |
+
species_counts,
|
846 |
+
colnames = c("Species", "Number of Records", "Mean Income", "Mean NDVI"),
|
847 |
+
options = list(pageLength = 10),
|
848 |
+
rownames = FALSE
|
849 |
+
)
|
850 |
+
})
|
851 |
+
|
852 |
+
# ------------------------------------------------
|
853 |
+
# Ggplot: Biodiversity & Socioeconomic Summary
|
854 |
+
# ------------------------------------------------
|
855 |
+
output$bioSocPlot <- renderPlot({
|
856 |
+
df <- socio_data()
|
857 |
+
if (nrow(df) == 0) return(NULL)
|
858 |
+
|
859 |
+
df_plot <- df %>%
|
860 |
+
mutate(IsoLabel = paste0(Mode, "-", Time, "min"))
|
861 |
+
|
862 |
+
ggplot(df_plot, aes(x = IsoLabel)) +
|
863 |
+
geom_col(aes(y = GBIF_Species), fill = "steelblue", alpha = 0.7) +
|
864 |
+
geom_line(aes(y = EstimatedPopulation / 1000, group = 1), color = "red", size = 1) +
|
865 |
+
geom_point(aes(y = EstimatedPopulation / 1000), color = "red", size = 3) +
|
866 |
+
labs(
|
867 |
+
x = "Isochrone (Mode-Time)",
|
868 |
+
y = "Blue bars: Unique Species \n | Red line: Population (thousands)",
|
869 |
+
title = "Biodiversity & Socioeconomic Summary"
|
870 |
+
) +
|
871 |
+
theme_minimal(base_size = 14) +
|
872 |
+
theme(
|
873 |
+
axis.text.x = element_text(angle = 45, hjust = 1, size = 12),
|
874 |
+
axis.text.y = element_text(size = 12),
|
875 |
+
axis.title.x = element_text(size = 14),
|
876 |
+
axis.title.y = element_text(size = 14)
|
877 |
+
)
|
878 |
+
})
|
879 |
+
|
880 |
+
# ------------------------------------------------
|
881 |
+
# Bar plot: GBIF records by institutionCode
|
882 |
+
# ------------------------------------------------
|
883 |
+
output$collectionPlot <- renderPlot({
|
884 |
+
iso_data <- isochrones_data()
|
885 |
+
if (is.null(iso_data) || nrow(iso_data) == 0) {
|
886 |
+
plot.new()
|
887 |
+
title("No GBIF records found in this isochrone.")
|
888 |
+
return(NULL)
|
889 |
+
}
|
890 |
+
|
891 |
+
iso_union <- st_union(iso_data)
|
892 |
+
inter_gbif <- st_intersection(sf_gbif, iso_union)
|
893 |
+
if (nrow(inter_gbif) == 0) {
|
894 |
+
plot.new()
|
895 |
+
title("No GBIF records found in this isochrone.")
|
896 |
+
return(NULL)
|
897 |
+
}
|
898 |
+
|
899 |
+
df_code <- inter_gbif %>%
|
900 |
+
st_drop_geometry() %>%
|
901 |
+
group_by(institutionCode) %>%
|
902 |
+
summarize(count = n(), .groups = "drop") %>%
|
903 |
+
arrange(desc(count))
|
904 |
+
|
905 |
+
ggplot(df_code, aes(x = reorder(institutionCode, -count), y = count)) +
|
906 |
+
geom_bar(stat = "identity", fill = "darkorange", alpha = 0.7) +
|
907 |
+
labs(
|
908 |
+
x = "Institution Code",
|
909 |
+
y = "Number of Records",
|
910 |
+
title = "GBIF Records by Institution Code (Isochrone Union)"
|
911 |
+
) +
|
912 |
+
theme_minimal(base_size = 14) +
|
913 |
+
theme(
|
914 |
+
axis.text.x = element_text(angle = 45, hjust = 1, size = 12),
|
915 |
+
axis.text.y = element_text(size = 12),
|
916 |
+
axis.title.x = element_text(size = 14),
|
917 |
+
axis.title.y = element_text(size = 14)
|
918 |
+
)
|
919 |
+
})
|
920 |
+
|
921 |
+
# ------------------------------------------------
|
922 |
+
# Additional Section: mapview for species richness vs. data availability
|
923 |
+
# ------------------------------------------------
|
924 |
+
output$mapNUI <- renderUI({
|
925 |
+
map_n <- mapview(cbg_vect_sf, zcol = "n", layer.name="Data Availability (n)")
|
926 |
+
map_n@map
|
927 |
+
})
|
928 |
+
|
929 |
+
output$mapSpeciesUI <- renderUI({
|
930 |
+
map_s <- mapview(cbg_vect_sf, zcol = "n_species", layer.name="Species Richness (n_species)")
|
931 |
+
map_s@map
|
932 |
+
})
|
933 |
+
|
934 |
+
# ------------------------------------------------
|
935 |
+
# Additional Plot: n_observations vs n_species
|
936 |
+
# ------------------------------------------------
|
937 |
+
output$obsVsSpeciesPlot <- renderPlot({
|
938 |
+
# A simple scatter plot of n_observations vs. n_species from cbg_vect_sf
|
939 |
+
ggplot(cbg_vect_sf, aes(x = log(n_observations+1), y = log(unique_species+1)) ) +
|
940 |
+
geom_point(color = "blue", alpha = 0.6) +
|
941 |
+
labs(
|
942 |
+
x = "Number of Observations (n_observations)",
|
943 |
+
y = "Number of Species (n_species)",
|
944 |
+
title = "Data Availability vs. Species Richness"
|
945 |
+
) +
|
946 |
+
theme_minimal(base_size = 14)
|
947 |
+
})
|
948 |
+
|
949 |
+
# ------------------------------------------------
|
950 |
+
# Additional Plot: Linear model of n_species ~ n_observations + median_inc + ndvi_mean
|
951 |
+
# ------------------------------------------------
|
952 |
+
# output$lmCoefficientsPlot <- renderPlot({
|
953 |
+
# # Build a linear model with cbg_vect_sf
|
954 |
+
# # Must ensure there are no NAs
|
955 |
+
# df_lm <- cbg_vect_sf %>%
|
956 |
+
# filter(!is.na(n_observations),
|
957 |
+
# !is.na(unique_species),
|
958 |
+
# !is.na(median_inc),
|
959 |
+
# !is.na(ndvi_mean))
|
960 |
+
#
|
961 |
+
# if (nrow(df_lm) < 5) {
|
962 |
+
# # not enough data
|
963 |
+
# plot.new()
|
964 |
+
# title("Not enough data for linear model.")
|
965 |
+
# return(NULL)
|
966 |
+
# }
|
967 |
+
#
|
968 |
+
# # Model
|
969 |
+
# fit <- lm(unique_species ~ n_observations + median_inc + ndvi_mean, data = df_lm)
|
970 |
+
#
|
971 |
+
# # Using sjPlot to visualize coefficients
|
972 |
+
# # We store in an object and then print it
|
973 |
+
# p <- plot_model(fit, show.values = TRUE, value.offset = .3, title = "LM Coefficients: n_species ~ n_observations + median_inc + ndvi_mean")
|
974 |
+
# print(p)
|
975 |
+
# })
|
976 |
+
}
|
977 |
+
|
978 |
+
shinyApp(ui, server)
|
979 |
+
|
980 |
+
|
981 |
+
|
982 |
+
# library(profvis)
|
983 |
+
#
|
984 |
+
# profvis({
|
985 |
+
# shinyApp(ui, server)
|
986 |
+
# })
|
R/rsconnect/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
R/rsconnect/shinyapps.io/diego-ellis-soto/RSF_Biodiversity_Access.dcf
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: RSF_Biodiversity_Access
|
2 |
+
title: RSF_Biodiversity_Access
|
3 |
+
username: diego-ellis-soto
|
4 |
+
account: diego-ellis-soto
|
5 |
+
server: shinyapps.io
|
6 |
+
hostUrl: https://api.shinyapps.io/v1
|
7 |
+
appId: 13693040
|
8 |
+
bundleId: 9630454
|
9 |
+
url: https://diego-ellis-soto.shinyapps.io/RSF_Biodiversity_Access/
|
10 |
+
version: 1
|
11 |
+
asMultiple: FALSE
|
12 |
+
asStatic: FALSE
|
www/California_academy_logo.png
ADDED
![]() |
www/Reimagining_San_Francisco.png
ADDED
![]() |
www/UC Berkeley_logo.png
ADDED
![]() |