在 Shiny Leaflet 应用程序中,用户输入被解释为弹出窗口中的文本而不是列名

in Shiny Leaflet app, user input interpreted as text in popup instead of a column name

我希望用户能够通过从下拉列表中选择不同的属性来更改等值线图叠加层。地图和不同的属性根据用户选择呈现良好,但 addPolygons 函数的弹出选项不会像应用程序的其余部分一样解释输入值。

对于下面的内容,假设我们正在使用一个名为 densitydata 的 class 'sf' 对象,其中包含多边形地理列和要映射的不同密度(dens1、dens2 和 dens3) ).我希望弹出窗口 return 为单击的特征选择的密度值。例如。 “选定密度:1.23”,而是获取列名称:“选定密度:dens1”。

如果我将输入放入像 round() 这样的数学函数中,我会收到一个错误,提示我向数学函数提供了一个非数字值:

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

下拉菜单:


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), 

在服务器函数中渲染地图:


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" )
      
  } )

不能直接使用input$densities,可以通过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" )
      
  } )