首页 > 解决方案 > 传单 + 闪亮:addLegend 值中的“参数长度为零”

问题描述

我正在开发一个闪亮的应用程序来显示等值线图,使用传单和 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()),但无济于事。

外面闪亮的东西与下面的代码配合得很好(我一直在使用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)

我错过了什么吗?

标签: rshinychoroplethr-leaflet

解决方案


推荐阅读