rshinypopupr-leaflet

User input interpreted as text in popup instead of a column name


I would like users to be able to change the choropleth map overlay by selecting different attributes from a dropdown. The map and different attributes render fine based on user selection, but the popup option of the addPolygons function does not interpret the input values the same as the rest of the app.

For below, assume we are using an object of class 'sf' named densitydata with columns for the polygon geography and the different densities to be mapped (dens1, dens2 and dens3). I want the popup to return the values of the selected density for the clicked feature. E.g. "Selected density: 1.23" but instead get the name of the column: "Selected density: dens1".

If I put the input in a mathematical function like round(), I get an error that I'm providing a non-numeric value to a mathematical function:

popup = ~paste0("<b>Selected density:</b> ", round(input$densities, 2))

Dropdown menu:


dashboardSidebar(
    
    selectInput(inputId = "densities", label = "Select attribute", 
                choices = c("Density 1" = "dens1",
                            "Density 2" = "dens2",
                            "Density 3" = "dens3"), selected = NULL, multiple = F, selectize = TRUE, width = '500px', size = NULL), 

Rendering the map in the server function:


output$DensMap <- renderLeaflet({
    
# Define palette for new selection
    DensPal <- colorQuantile(palette = "YlOrRd", 
                             domain = {densitydata %>%
                                 pull(input$densities)}, 
                             n = 5)
 
# Render map
    leaflet(data = densitydata) %>%
      addTiles() %>%  # Add default OpenStreetMap map tiles
      addPolygons(fillColor = ~DensPal({densitydata %>% pull(input$densities)}),
                  fillOpacity = 0.5, 
                  weight = 2,
                  popup = ~paste0("<b>Selected density:</b> ", input$densities) %>%
      addLegend(pal = DensPal, 
                values = {densitydata %>% pull(input$densities)}, 
                opacity=0.7, 
                title = "Legend title here", 
                position = "bottomleft" )
      
  } )

Solution

  • You can not use input$densities directly, you can use it thru densitydata[[input$densities]]:

    output$DensMap <- renderLeaflet({
        
    # Define palette for new selection
        DensPal <- colorQuantile(palette = "YlOrRd", 
                                 domain = densitydata[[input$densities]], 
                                 n = 5)
     
    # Render map
        leaflet(data = densitydata) %>%
          addTiles() %>%  # Add default OpenStreetMap map tiles
          addPolygons(fillColor = ~DensPal(densitydata[[input$densities]]),
                      fillOpacity = 0.5, 
                      weight = 2,
                      popup = ~paste0("<b>Selected density:</b> ", 
                                      densitydata[[input$densities]]) %>%
          addLegend(pal = DensPal, 
                    values = densitydata[[input$densities]], 
                    opacity=0.7, 
                    title = "Legend title here", 
                    position = "bottomleft" )
          
      } )