fish_cas / app.R
Otolith's picture
Upload 4 files
c04ce03 verified
library(shiny)
library(sf)
library(stringr)
library(dplyr)
library(leaflet)
options(sf_use_s2 = FALSE)
# Load the shapefiles and data
cas <- readRDS("cas.rds")
inland <- readRDS("inland.rds")
marine <- readRDS("marine.rds")
# Example species list
species_list <- sort(unique(cas$valid_name))
ui <- fluidPage(
titlePanel("Global fish distribution across biogeographic realm"),
tags$head(tags$style(HTML("
body { background-color: #f2f2f2; color: #333333; }
.well { background-color: #ffffff; border: none; }
#map { height: 100vh; } /* Set the map to take the full viewport height */
#controls {
background-color: rgba(255, 255, 255, 0.3);
padding: 15px;
border-radius: 8px;
}
"))),
fluidRow(
column(2, # Left side navigation panel (20% of the width)
div(id = "controls", class = "panel panel-default",
fixed = FALSE, draggable = TRUE,
width = "100%", height = "100vh",
#img(src = "path_to_logo.png", height = "100px"), # Replace with actual logo path
selectizeInput("species",
HTML(paste0("Choose/Input a species (<span style='color:red;'>", scales::comma(nrow(cas)), "</span>):")),
choices = NULL,
selected = NULL),
actionButton("run", "Run", class = "btn-primary")
),
div(
tags$h5(HTML("<strong>Acknowledgements</strong>:")),
tags$p(HTML("<small>Heartfelt thanks to the hundreds of taxonomists for their selfless dedication. Data source (updated June 2024): <a href='http://researcharchive.calacademy.org/research/ichthyology/catalog/fishcatmain.asp' target='_blank'> Eschmeyer's Catalog of Fishes</a>.</small>"))
),
div(
tags$h5(HTML("<strong>References</strong>:")),
tags$p(HTML("<small>[1]Fricke R, Eschmeyer WN, Van der Laan R.(2024). <a href='http://researcharchive.calacademy.org/research/ichthyology/catalog/fishcatmain.asp' ",
"target='_blank'>Catalog of fishes: genera, species, references </a>. California Academy of Sciences, San Francisco, CA, USA.</small>")),
tags$p(HTML("<small>[2]Costello MJ, Tsai P, Wong PS, Cheung AKL, Basher Z, Chaudhary C.(2017). Marine biogeographic realms and species endemicity. <em>Nature Communications</em>, 8(1), 1057. </small>")),
tags$p(HTML("<small>[3]Tedesco PA, Beauchard O, Bigorne R, Blanchet S, Buisson L, Conti L, Cornu J,Dias MS, Grenouillet G, Hugueny B, Jézéquel C, Leprieur F, Brosse S, Oberdorff T.(2017). A global database on freshwater fish species occurrence in drainage basins. <em>Scientific Data</em>, 4(1), 1-6.</small>"))
),
tags$map(HTML(paste0(
'<script type="text/javascript" id="clstr_globe" src="//clustrmaps.com/globe.js?d=fcrVhJisR3nagD85u6uUHpeDl2frZFCBy5i5BhDcpss"></script>'
))),
),
column(10, # Right side map panel (80% of the width)
leafletOutput("map", width = "100%", height = "100vh")
)
)
)
# Server logic
server <- function(input, output, session) {
# Update selectize input with species list (server-side)
updateSelectizeInput(session, "species",
choices = species_list,
server = TRUE)
observeEvent(input$run, {
sp <- input$species
realm <- cas %>%
dplyr::filter(valid_name %in% sp) %>%
dplyr::select(biogeographic_realm) %>%
str_split(";") %>%
unlist()
# Create map with Leaflet
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addPolygons(data = marine[which(marine$name %in% realm),],
fillColor = "#00A8E6", fillOpacity = 0.5, color = NA, group = "Marine") %>%
addPolygons(data = inland[which(inland$BasinName %in% realm),],
fillColor = "#EEB134", fillOpacity = 0.5, color = NA, group = "Inland") %>%
#setView(lng = 0, lat = 20, zoom = 2) %>%
addControl(
html = paste(
"<strong>Data Source (Updated 2024-06): ",
"<a href='http://researcharchive.calacademy.org/research/ichthyology/catalog/fishcatmain.asp' ",
"target='_blank'>Eschmeyer's Catalog of Fishes</a></strong><br>",
"<small>Feel free to contact: <a href='mailto:[email protected]'>[email protected]</a></small>"
),
position = "bottomright",
className = "leaflet-control-custom"
)
})
})
# Add a popup showing species name and biogeographic realm on map click
observeEvent(input$map_click, {
click <- input$map_click
if (is.null(click))
return()
# Extract the species Latin name and realm for popup
sp <- input$species
habitat <- cas %>%
dplyr::filter(valid_name %in% sp) %>%
dplyr::select(habitat)
leafletProxy("map") %>%
clearPopups() %>%
addPopups(
lng = click$lng, lat = click$lat,
popup = paste0(
"<strong>Species: </strong>", "<em>", sp, "</em>",
"<br><strong>Habitat: </strong>", habitat
)
)
})
}
# Run the app
shinyApp(ui, server)