-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
82 lines (73 loc) · 2.13 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
server <- function(input, output, session) {
densities_filter <- reactive({
dplyr::filter(
densities,
Group %in% input$species,
Month %in% input$period
)
})
geo_filter <- reactive({
# Create bounding box
bbox <- c(
xmin = input$lon[1], ymin = input$lat[1],
xmax = input$lon[2], ymax = input$lat[2]
) |>
sf::st_bbox(crs = sf::st_crs(4326)) |>
sf::st_as_sfc()
# Intersect with atlas grid
geo[bbox, ]
})
# Density of all birds selected during all seasons selected in specified bbox
geo_data <- reactive({
dat <- dplyr::group_by(
densities_filter(),
Stratum
) |>
dplyr::summarize(Density = sum(Density, na.rm = TRUE))
# Join with spatial data
dplyr::left_join(geo_filter(), dat, by = c("id" = "Stratum")) |>
dplyr::select(Density)
})
summary_table <- reactive({
dplyr::group_by(densities_filter(), Group, Month) |>
dplyr::summarise(Density = round(sum(Density, na.rm = TRUE), 2)) |>
tidyr::pivot_wider(names_from = Month, values_from = Density)
})
output$table <- renderDataTable(
densities_filter(),
rownames = FALSE,
options = list(pageLength = 10)
)
output$map <- renderLeaflet({
# Color palette
rgeo <- range(geo_data()$Density, na.rm = TRUE)
pal <- leaflet::colorNumeric(
viridis::viridis_pal(option = "D")(100),
domain = rgeo
)
# Map
leaflet(geo_data()) |>
setView(lng = -55.5, lat = 60, zoom = 4) |>
addProviderTiles("CartoDB.Positron") |>
addPolygons(
opacity = 1,
weight = 1,
color = ~ pal(geo_data()$Density)
) |>
addLegend(
position = "bottomright",
pal = pal,
values = seq(rgeo[1], rgeo[2], length.out = 5),
opacity = 1,
title = "Bird density"
)
})
output$nspecies <- renderText(length(input$species))
output$nperiods <- renderText(length(input$period))
output$mn_density <- renderText(round(mean(geo_data()$Density, na.rm = TRUE), 2))
output$summary <- renderTable(
summary_table(),
rownames = FALSE,
options = list(pageLength = 10)
)
}