rmapsr-leafletmapview

How to visualize single points in shiny app mapview leaflet


I have a large dataset but I am providing 3 records to make this example reproducible with the minimum amount of data. I am able to show on leaflet all the 3 sites at once but I would like to show one record at a time as well. My question is, how can I manage to show on the map one site at the time when the input dropdown is clicked.

For example, how can I show site2 only instead of showing all the 3 records? I am aware of 'switch' but don't know how to implement it here.

I have some code below and reproducible data.

  Location            geometry
* <chr>            <POINT [°]>
1 site1    (-115.1682 44.3943)
2 site2    (-114.0403 44.6845)
3 site3    (-114.8841 44.1533)

library(shiny)
library(mapview)
library(writexl)
library(tidyverse)
library(leaflet)

locations <- c("site1", "site2", "site3") 
 ui <- fluidPage(

#titlePanel("Screwtraps and temp logger in Idaho"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "site",
           label = "Select Site",
                   choices = locations)),
    mainPanel(leafletOutput("map",width = "100%", height="87vh") 
    )
   )
 )
 
 server <- function(input, output, session) {
  session$onSessionEnded(function() {
    stopApp()
  })
  
  output$map<-renderLeaflet({
 #Select all sites
    final <- mapview(site, col.regions='red', alpha=1) 
   final@map 
   #How can I render my map with only the selected site from the userInput dropdown?

    })
}

shinyApp(ui, server)


Data is here: 

site <- structure(list(Location = c("site1", "site2", "site3"), geometry = structure(list(
    structure(c(-115.1682, 44.3943), class = c("XY", "POINT", 
    "sfg")), structure(c(-114.0403, 44.6845), class = c("XY", 
    "POINT", "sfg")), structure(c(-114.8841, 44.1533), class = c("XY", 
    "POINT", "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
    input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n    DATUM[\"World Geodetic System 1984\",\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), bbox = structure(c(xmin = -115.1682, 
ymin = 44.1533, xmax = -114.0403, ymax = 44.6845), class = "bbox"), class = c("sfc_POINT", 
"sfc"))), row.names = c(NA, 3L), sf_column = "geometry", agr = structure(c(Location = NA_integer_), class = "factor", .Label = c("constant", 
"aggregate", "identity")), class = c("sf", "tbl_df", "tbl", "data.frame"
))

site

enter image description here


Solution

  • In case you want to select one site, more than one site, or all sites, you can use pickerInput from shinyWidgets package. To show a single (or more than one) site, you just need to subset your data. With pickerInput, it is easier to select some or none. Hope this is helpful.

    library(shiny)
    library(shinyWidgets)
    library(mapview)
    library(leaflet)
    
    locations <- c("site1", "site2", "site3") 
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          pickerInput(inputId = "site",
                      label = "Select Site",
                      choices = locations,
                      selected = locations,
                      options = list(`actions-box` = TRUE),
                      multiple = TRUE)),
        mainPanel(leafletOutput("map",width = "100%", height="87vh") 
        )
      )
    )
    
    server <- function(input, output, session) {
      
      output$map<-renderLeaflet({
        req(input$site)
        final <- mapview(site[site$Location %in% input$site, ], 
                         col.regions = 'red',
                         layer.name = "Site",
                         alpha = 1) 
        final@map 
      })
      
    }
    
    shinyApp(ui, server)