-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
141 lines (100 loc) · 9.53 KB
/
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
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
# by A. Marcia Barbosa (https://modtools.wordpress.com/barbosa/)
# used for https://atlasmamiferosportugal.wordpress.com/
# last updated 31 Jul 2023
# RStudio: Session -> Set Working Directory -> To Source File Location
library(terra)
library(leaflet)
library(leaflet.extras) # 'addSearchOSM' function
library(shiny)
# carregar objectos necessarios tanto para 'ui' como para 'server':
source('global.R')
ui <- fluidPage(
fluidPage(
h1("Atlas of Mammals in Portugal") # or titlePanel()
),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "ordem", label = "Order", choices = c("ALL", as.character(sort(unique(atlas$ordem))))),
selectInput(inputId = "especie", label = "Species", choices = "", selectize = FALSE),
# hr(), # horizontal line
p("Note that", tags$i("Microtus agrestis"), "populations in Portugal were recently reassigned to the new species", tags$i("Microtus rozianus", .noWS = "after"), "."),
br(),
p(tags$strong("SOURCES:")),
p("Bencatel J., Sabino-Marques H., Álvares F., Moura A.E. & Barbosa A.M. (eds.) (2019)", a(href = "https://atlasmamiferosportugal.wordpress.com/", "Atlas de Mamiferos de Portugal (2nd edition)", .noWS = "after"), ". Universidade de Evora, Portugal."),
p("Grilo C. et al. [numerous co-authors] (2022)", a(href = "https://doi.org/10.1002/ecy.3654", "Mammals in Portugal: A data set of terrestrial, volant, and marine mammal occurrences in Portugal", .noWS = "after"), ".", tags$i("Ecology,"), "103:e3654."),
p("Mathias M.L. (coord.), Fonseca C., Rodrigues L., Grilo C., Lopes-Fernandes M., Palmeirim J.M., Santos-Reis M., Alves P.C., Cabral J.A., Ferreira M., Mira A., Eira C., Negrões N., Paupério J., Pita R., Rainho A., Rosalino L.M., Tapisso J.T. & Vingada J. (eds.) (2023)", a(href = "https://livrovermelhodosmamiferos.pt/", "Livro Vermelho dos Mamíferos de Portugal Continental", .noWS = "after"), ". FCiências.ID, ICNF, Lisboa. [Data downloaded from", a(href = "https://www.gbif.org/occurrence/download/0017776-230828120925497", "GBIF - ICNF Mammal Red Book collections", .noWS = "after"), " and filtered to exclude records with spatial uncertainty larger than 10 km2]"),
br(),
p("Note that presences from Grilo et al. (2022) and Mathias et al. (2023) are unselected by default, to avoid saturating the map; mouse over the 'layers' menu on the top right to add them. Note also that these sources originally provide point occurrences at a higher resolution; here they were converted to the Atlas grid, but see the original publications for more detail."),
), # end sidebar panel
mainPanel(
leafletOutput(outputId = "mapa", height = "80vh"),
p("The data, maps and code behind this webpage are available at", a(href = "https://github.com/AMBarbosa/AtlasMamPor", "https://github.com/AMBarbosa/AtlasMamPor"), "under a Creative Commons Attribution-ShareAlike license (CC BY-SA 4.0)."),
p("The Atlas of Mammals in Portugal gathered mammal occurrence records on UTM 10x10-km2 grid cells, made available (in publications, theses, reports, online photos or direct contributions) over approximately three decades up to 2018. Mind that survey effort was uneven: as in most atlases, the data reflect spatial and taxonomic bias, as some species and areas were more intensively surveyed than others. The atlas is a picture of the knowledge made available up to the time of publication."),
p("Grid cell presences extracted from major datasets published after the Atlas were added to the interactive maps (you can select them after unfolding the top-right menu), and are now also cited under 'SOURCES' on the left panel"),
p("Note that the 'confirmed', 'credible' and 'interview' labels refer only to the Atlas records in dark red. Note also that marine mammals are not in the interactive maps due to data privacy, but they have plenty of records in the Atlas. See the PDF book (freely available", a(href = "https://atlasmamiferosportugal.wordpress.com", "here", .noWS = "after"), ") for more info on the data!")
) # end mainPanel
) # end sidebarLayout
) # end fluidPage
server <- function(input, output, session) {
seleccionar_especies <- reactive({
ord <- input$ordem
if (ord == "ALL") especies <- sort(unique(atlas$especie))
else especies <- sort(unique(atlas[atlas$ordem == ord, "especie"]))
especies
})
observe({
updateSelectInput(session, "especie", choices = seleccionar_especies()
)})
seleccionar_dados <- reactive({
dados <- dados[dados$especie == input$especie, ] # 'dados' (from global.R) includes atlas, datapaper, redbook
dados
})
limites <- terra::ext(ptgal)
cor_grelha <- "darkgrey"
cor_semdata <- "grey"
cor_antigo <- "orange"
cor_recente <- "darkred"
cor_confirm <- "#FF6347"
cor_datapaper <- "darkblue"
cor_redbook <- "red"
# https://stackoverflow.com/questions/37862467/leaflet-legend-for-custom-markers-in-r
# images need to be online or stored in a folder named 'www'
atlas_legend <- "<img src='legenda_semdata.png'>No date<br/>
<img src='legenda_antigo.png'>Old (1990-1999)<br/>
<img src='legenda_recente.png'>Recent (2000-2018)<br/>
<img src='legenda_confirmado.png'> confirmed<br/>
<img src='legenda_plausivel.png'> credible<br/>
<img src='legenda_inquerito.png'> interview<br/>
<img src='legenda_datapaper.png'> Grilo et al. (2022)<br/>
<img src='legenda_redbook.png'> Mathias et al. (2023)<br/>"
output$mapa <- renderLeaflet({
leaflet(data = ptgal) |>
fitBounds(lng1 = as.numeric(limites[1]), lat1 = as.numeric(limites[3]), lng2 = as.numeric(limites[2]), lat2 = as.numeric(limites[4])) |>
addTiles(options = providerTileOptions(minZoom = 4, maxZoom = 15), group = "OpenStreetMap") |>
addProviderTiles(options = providerTileOptions(minZoom = 4, maxZoom = 15), providers$OpenTopoMap, group = "OpenTopoMap") |>
addProviderTiles(options = providerTileOptions(minZoom = 4, maxZoom = 15), providers$Esri.WorldImagery, group = "Esri.WorldImagery") |>
addControl(html = atlas_legend, position = "bottomleft")
}) # end renderLeaflet
observe({
dados <- seleccionar_dados()
leafletProxy("mapa", data = ptgal) |>
clearShapes() |>
# https://search.r-project.org/cran/refmans/leaflet/html/addMapPane.html
addMapPane("back", zIndex = 410) |> # always below
addMapPane("front", zIndex = 440) |> # always on top, for grid utm labels to show
addPolygons(data = ptgal[ptgal$utm10 %in% dados[which(is.na(dados$recente) & dados$source == "atlas"), "utm10"], ], fillColor = cor_semdata, fillOpacity = 0.5, stroke = FALSE, group = "No date", options = pathOptions(pane = "back")) |>
addPolygons(data = ptgal[ptgal$utm10 %in% dados[!is.na(dados$recente) & dados$recente == 0 & dados$source == "atlas", "utm10"], ], fillColor = cor_antigo, fillOpacity = 0.6, stroke = FALSE, group = "Old", options = pathOptions(pane = "back")) |>
addPolygons(data = ptgal[ptgal$utm10 %in% dados[!is.na(dados$recente) & dados$recente == 1 & dados$source == "atlas", "utm10"], ], fillColor = cor_recente, fillOpacity = 0.6, stroke = FALSE, group = "Recent", options = pathOptions(pane = "back")) |>
addCircles(data = ptgal[ptgal$utm10 %in% dados[!is.na(dados$confirmado) & dados$confirmado == 1, "utm10"], c("centr_x", "centr_y")], ~centr_x, ~centr_y, radius = 3000, stroke = FALSE, fill = TRUE, fillColor = cor_confirm, fillOpacity = 0.5, group = "Reliability", options = pathOptions(pane = "back")) |>
addCircles(data = ptgal[ptgal$utm10 %in% dados[!is.na(dados$confirmado) & dados$confirmado == 0, "utm10"], c("centr_x", "centr_y")], ~centr_x, ~centr_y, radius = 3000, stroke = TRUE, fill = FALSE, color = cor_confirm, weight = 2, opacity = 0.5, group = "Reliability", options = pathOptions(pane = "back")) |>
addRectangles(data = ptgal[ptgal$utm10 %in% dados[!is.na(dados$confirmado) & dados$confirmado == -1, "utm10"], c("centr_x", "centr_y")], lng1 = ~centr_x - 0.03, lat1 = ~centr_y - 0.008, lng2 = ~centr_x + 0.03, lat2 = ~centr_y + 0.008, stroke = FALSE, fill = TRUE, fillColor = cor_confirm, fillOpacity = 0.5, group = "Reliability", options = pathOptions(pane = "back")) |>
# add external (data paper + red book):
addRectangles(data = ptgal[ptgal$utm10 %in% dados[dados$source == "datapaper", "utm10"], ], ~centr_x, ~centr_y, lng1 = ~centr_x - 0.03, lat1 = ~centr_y - 0.025, lng2 = ~centr_x + 0.03, lat2 = ~centr_y + 0.025, stroke = FALSE, fill = TRUE, fillColor = cor_datapaper, weight = 2, fillOpacity = 0.5, group = "Grilo et al. (2022)", options = pathOptions(pane = "back")) |>
addRectangles(data = ptgal[ptgal$utm10 %in% dados[dados$source == "redbook", "utm10"], ], ~centr_x, ~centr_y, lng1 = ~centr_x - 0.03, lat1 = ~centr_y - 0.025, lng2 = ~centr_x + 0.03, lat2 = ~centr_y + 0.025, stroke = FALSE, fill = TRUE, fillColor = cor_redbook, weight = 2, fillOpacity = 0.7, group = "Mathias et al. (2023)", options = pathOptions(pane = "back")) |>
addPolygons(color = cor_grelha, fillColor = NULL, fillOpacity = 0, weight = 1, label = ~utm10, labelOptions = labelOptions(noHide = FALSE, textOnly = FALSE, opacity = 0.8, textsize = "16px", style = list("color" = "darkgreen")), options = pathOptions(pane = "front")) |>
addLayersControl(overlayGroups = c("No date", "Old", "Recent", "Reliability", "Grilo et al. (2022)", "Mathias et al. (2023)"), baseGroups = c("OpenStreetMap", "OpenTopoMap", "Esri.WorldImagery"), options = layersControlOptions(collapsed = TRUE)) |>
hideGroup(c("Grilo et al. (2022)", "Mathias et al. (2023)")) |>
leaflet.extras::addSearchOSM()
}) # end observe
}
shinyApp(ui = ui, server = server)