单张+闪亮:addLegend值中的“参数长度为零”

时间:2019-11-02 20:35:14

标签: r shiny choropleth r-leaflet

我正在开发一个闪亮的应用程序,以显示传单的地图,使用传单和shapefile来添加多边形。外面有光泽,一切正常,但是有了光泽,在使用addLegend时出现如下问题:

mypal <- reactive({
        colorBin(palette = "RdYlBu", domain = get("map_fires")[[paste0("qm_", selectedYear())]], 
                           bins = 5, reverse = TRUE, pretty = FALSE,
                           na.color = "#FFFAFA")
    })


output$map <- renderLeaflet({
        leaflet() %>% 
            addProviderTiles("OpenStreetMap.Mapnik") %>%
            addPolygons(data = map_fires,
                        stroke = TRUE, weight = 1, smoothFactor = 0.2, fillOpacity = 0.3,
                        fillColor = ~mypal()(get("map_fires")[[paste0("qm_", selectedYear())]]),
                        highlight = highlightOptions(
                            weight = 5,
                            color = "#666",
                            fillOpacity = 0.7,
                            bringToFront = TRUE),
                        label= lapply(stats_labels(), HTML), 
                        labelOptions = labelOptions(
                            style = list("font-weight" = "normal", padding = "3px 8px"),
                            textsize = "15px",
                            direction = "auto")) %>%
            addLegend(position = "bottomright", pal = ~mypal(), 
                      values = ~get("map_fires")[[paste0("qm_", selectedYear())]], 
                      opacity = 1)
    })

上面的代码返回:

Warning: Error in if: argument is of length zero
  104: addLegend
...

我正在使用paste0("qm_", selectedYear())来引用具有年度数据的不同列。

我相信问题出在values = ~get("map_fires")[[paste0("qm_", selectedYear())]],但不能说出原因。我已经尝试在addLegend上指定数据集,并在values =上放置特定的列(即,不依赖于selectedYear()的反应值),无济于事。 / p>

下面的代码可以很好地处理外部问题(我继续使用get("map_fires")[[paste0("qm_", 2018)]],在特定年份更改selectedYear(),只是为了更好地展示我的情况)

mypal <- colorBin(palette = "RdYlBu", domain = get("map_fires")[[paste0("qm_", 2018)]], 
                  bins = 5, reverse = TRUE, pretty = FALSE,
                  na.color = "#FFFAFA")

leaflet() %>% 
    addProviderTiles("OpenStreetMap.Mapnik") %>%
    addPolygons(data = map_fires,
                stroke = TRUE, weight = 1, smoothFactor = 0.2, fillOpacity = 0.3,
                fillColor = ~mypal(get("map_fires")[[paste0("qm_", 2018)]]),
                highlight = highlightOptions(
                    weight = 5,
                    color = "#666",
                    fillOpacity = 0.7,
                    bringToFront = TRUE),
                label= lapply(stats_labels, HTML), 
                labelOptions = labelOptions(
                    style = list("font-weight" = "normal", padding = "3px 8px"),
                    textsize = "15px",
                    direction = "auto")) %>%
    addLegend(position = "bottomright", pal = mypal, 
              values = get("map_fires")[[paste0("qm_", 2018)]],
              opacity = 1)

我想念什么吗?

0 个答案:

没有答案