-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiny_server.R
218 lines (200 loc) · 7.31 KB
/
shiny_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
# the app logic
server <- function(input, output, session) {
filtered_data <- eventReactive(input$update, {
req(input$year)
if (input$single_species) {
req(input$species_single)
result <- grid_data %>%
dplyr::filter(
species == input$species_single,
year == input$year
)
} else {
req(input$species1, input$species2)
result <- grid_data %>%
dplyr::filter(
species %in% c(input$species1, input$species2),
year == input$year
)
}
# Function to set map bounds
set_map_bounds <- function(lat1, lat2, lon1, lon2) {
leafletProxy("map") %>%
fitBounds(
lng1 = lon1,
lat1 = lat1,
lng2 = lon2,
lat2 = lat2
)
}
# Observers for zoom buttons
observeEvent(input$zoom_ra1, {
set_map_bounds(
lat1 = 53,
lat2 = 59,
lon1 = -20, # Note: negative for western longitude
lon2 = -13
)
})
observeEvent(input$zoom_ra2, {
set_map_bounds(
lat1 = 65,
lat2 = 72,
lon1 = -6, # West
lon2 = 8 # East
)
})
observeEvent(input$zoom_ra3, {
set_map_bounds(
lat1 = 72,
lat2 = 77,
lon1 = 30,
lon2 = 45
)
})
result
})
output$map <- renderLeaflet({
leaflet(options = leafletOptions(zoomControl = FALSE)) %>%
addTiles(
urlTemplate = "https://server.arcgisonline.com/ArcGIS/rest/services/Ocean/World_Ocean_Base/MapServer/tile/{z}/{y}/{x}",
attribution = "Tiles © Esri — Sources: GEBCO, NOAA, CHS, OSU, UNH, CSUMB, National Geographic, DeLorme, NAVTEQ, and Esri"
) %>%
addTiles(
urlTemplate = "https://server.arcgisonline.com/ArcGIS/rest/services/Ocean/World_Ocean_Reference/MapServer/tile/{z}/{y}/{x}",
attribution = "Tiles © Esri — Sources: GEBCO, NOAA, CHS, OSU, UNH, CSUMB, National Geographic, DeLorme, NAVTEQ, and Esri"
) %>%
addPolygons(
data = neafc_areas,
fillColor = "transparent",
color = "black",
weight = 2,
opacity = 1,
fillOpacity = 0,
group = "NEAFC Areas"
) %>%
addGraticule(interval = 10, style = list(color = "#999", weight = 0.5, opacity = 0.5)) %>%
addGraticule(interval = 5, style = list(color = "#999", weight = 0.25, opacity = 0.3)) %>%
onRender("
function(el, x) {
console.log('Map render function called');
L.control.zoom({position:'topright'}).addTo(this);
// Create labels for coordinates
var latLabelTop = L.DomUtil.create('div', 'coordinate-labels lat-label-top');
var latLabelBottom = L.DomUtil.create('div', 'coordinate-labels lat-label-bottom');
var lonLabelLeft = L.DomUtil.create('div', 'coordinate-labels lon-label-left');
var lonLabelRight = L.DomUtil.create('div', 'coordinate-labels lon-label-right');
el.appendChild(latLabelTop);
el.appendChild(latLabelBottom);
el.appendChild(lonLabelLeft);
el.appendChild(lonLabelRight);
console.log('Labels created');
// Function to update coordinate labels
function updateCoordinateLabels() {
var bounds = this.getBounds();
var northLat = bounds.getNorth().toFixed(2);
var southLat = bounds.getSouth().toFixed(2);
var westLon = bounds.getWest().toFixed(2);
var eastLon = bounds.getEast().toFixed(2);
latLabelTop.innerHTML = Math.abs(northLat) + '°' + (northLat >= 0 ? 'N' : 'S');
latLabelBottom.innerHTML = Math.abs(southLat) + '°' + (southLat >= 0 ? 'N' : 'S');
lonLabelLeft.innerHTML = Math.abs(westLon) + '°' + (westLon >= 0 ? 'E' : 'W');
lonLabelRight.innerHTML = Math.abs(eastLon) + '°' + (eastLon >= 0 ? 'E' : 'W');
console.log('Updating labels:', northLat, southLat, westLon, eastLon);
}
// Update labels on initial load and whenever the map moves
this.on('load moveend', updateCoordinateLabels);
// Initial update
updateCoordinateLabels.call(this);
}")
})
observe({
req(input$update)
data <- filtered_data()
if (input$single_species) {
if (nrow(data) == 0 || all(is.na(data$total_catch))) {
leafletProxy("map") %>%
clearGroup("Catches") %>%
clearControls()
} else {
valid_data <- data %>%
dplyr::filter(!is.na(total_catch), total_catch > 0)
if (nrow(valid_data) > 0) {
pal <- colorNumeric("viridis", domain = range(valid_data$total_catch))
leafletProxy("map") %>%
clearGroup("Catches") %>%
clearControls() %>%
addRectangles(
data = valid_data,
lng1 = ~lon_bin, lat1 = ~lat_bin,
lng2 = ~ lon_bin + 0.05, lat2 = ~ lat_bin + 0.05,
fillColor = ~ pal(total_catch),
color = "transparent",
weight = 0,
opacity = 1,
fillOpacity = 0.7,
popup = ~ paste(species, ": ", round(total_catch, 2), " kg"),
group = "Catches"
) %>%
addLegend(
position = "bottomright",
pal = pal,
values = valid_data$total_catch,
title = paste(input$species_single, "Catch (kg) -", input$year),
opacity = 1
)
} else {
leafletProxy("map") %>%
clearGroup("Catches") %>%
clearControls()
}
}
} else {
data1 <- data %>% dplyr::filter(species == input$species1)
data2 <- data %>% dplyr::filter(species == input$species2)
if (nrow(data1) == 0 && nrow(data2) == 0) {
leafletProxy("map") %>%
clearGroup("Catches") %>%
clearControls() %>%
addLegend("bottomright",
colors = "gray",
labels = "No data",
title = paste("No data for", input$year),
opacity = 1
)
} else {
combined_data <- dplyr::full_join(
data1 %>% dplyr::select(lon_bin, lat_bin, catch1 = total_catch),
data2 %>% dplyr::select(lon_bin, lat_bin, catch2 = total_catch),
by = c("lon_bin", "lat_bin")
) %>%
tidyr::replace_na(list(catch1 = 0, catch2 = 0)) %>%
dplyr::mutate(color = get_cell_color(catch1, catch2))
leafletProxy("map") %>%
clearGroup("Catches") %>%
addRectangles(
data = combined_data,
lng1 = ~lon_bin, lat1 = ~lat_bin,
lng2 = ~ lon_bin + 0.05, lat2 = ~ lat_bin + 0.05,
fillColor = ~color,
color = "transparent",
weight = 0,
opacity = 1,
fillOpacity = 0.7,
popup = ~ paste(
input$species1, ": ", round(catch1, 2), " kg<br>",
input$species2, ": ", round(catch2, 2), " kg"
),
group = "Catches"
) %>%
clearControls() %>%
addLegend("bottomright",
colors = c("red", "blue", "purple"),
labels = c(input$species1, input$species2, "Both species"),
title = paste("Species Presence -", input$year),
opacity = 1
)
}
}
})
}