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 (", scales::comma(nrow(cas)), "):")),
choices = NULL,
selected = NULL),
actionButton("run", "Run", class = "btn-primary")
),
div(
tags$h5(HTML("Acknowledgements:")),
tags$p(HTML("Heartfelt thanks to the hundreds of taxonomists for their selfless dedication. Data source (updated June 2024): Eschmeyer's Catalog of Fishes."))
),
div(
tags$h5(HTML("References:")),
tags$p(HTML("[1]Fricke R, Eschmeyer WN, Van der Laan R.(2024). Catalog of fishes: genera, species, references . California Academy of Sciences, San Francisco, CA, USA.")),
tags$p(HTML("[2]Costello MJ, Tsai P, Wong PS, Cheung AKL, Basher Z, Chaudhary C.(2017). Marine biogeographic realms and species endemicity. Nature Communications, 8(1), 1057. ")),
tags$p(HTML("[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. Scientific Data, 4(1), 1-6."))
),
tags$map(HTML(paste0(
''
))),
),
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(
"Data Source (Updated 2024-06): ",
"Eschmeyer's Catalog of Fishes
",
"Feel free to contact: ly_ding@126.com"
),
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(
"Species: ", "", sp, "",
"
Habitat: ", habitat
)
)
})
}
# Run the app
shinyApp(ui, server)