File size: 5,425 Bytes
c04ce03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)