-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEffortDensity_app.R
89 lines (78 loc) · 2.63 KB
/
EffortDensity_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
# This app has a time slider that allows the user to view changes in fishing effort over time
library(shiny)
library(tidyverse)
library(sp)
library(leaflet)
library(raster)
# process data
rasterdata <- lapply(2016:2023, function(year) {
rasterfile <- paste0("rasters/EffortDensity", year, ".tif")
raster(rasterfile) %>% reclassify(cbind(-Inf, 0.01, NA), right=FALSE)
})
# start ui
ui <- fluidPage(
tags$style(HTML("
#map {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#time_slider {
position: absolute;
bottom: 20px;
left: 20px;
z-index: 1000; /* Ensure slider is above map */
}
#translucent_panel {
position: absolute;
bottom: 30px;
left: 20px;
background-color: rgba(255, 255, 255, 0.75); /* Translucent white */
padding: 10px;
border-radius: 5px;
z-index: 999; /* Ensure panel is below slider */
}
")),
leafletOutput("map", height="100vh"),
div(id="translucent_panel",
sliderInput("year", "Retrieval Year",
min = 2016, max = 2023, value = 2016, sep = "", round = TRUE, step = 1,
animate = TRUE))
) # end ui
# start server
server <- function(input, output) {
inityear <- reactive({
match(input$year, 2016:2023)
})
# Leaflet map
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("Esri.OceanBasemap", options = providerTileOptions(variant = "Ocean/World_Ocean_Base"), group = "Basemap") %>%
addProviderTiles("Esri.OceanBasemap", options = providerTileOptions(variant = "Ocean/World_Ocean_Reference"), group = "Basemap") %>%
setView(lng=-84, lat=27, zoom=7)
})
observe({
initial_year_index <- inityear()
if (is.na(initial_year_index)) {
stop("Invalid initial year selected.")
}
raster_values <- getValues(rasterdata[[initial_year_index]])
value_range <- range(raster_values, na.rm = TRUE)
qpal <- colorQuantile("Spectral",
domain=value_range,
n=10,
na.color="transparent",
reverse=TRUE)
leafletProxy("map") %>%
clearImages() %>%
clearControls() %>%
addRasterImage(rasterdata[[initial_year_index]],
colors=qpal) %>%
leaflet::addLegend(title=HTML("Fishing Effort Density<br>(hauls per sq km)"), position="topright",
pal=qpal, values=value_range, opacity=0.75 )
})
} # end server
# Run the app
shinyApp(ui = ui, server = server)