R 传单+;闪亮的:“我的朋友。”;参数的长度为“0”;添加图例值
我正在开发一个闪亮的应用程序来显示choropleth地图,使用传单和形状文件来添加多边形。外部shiny一切正常,但使用shiny时,我在使用R 传单+;闪亮的:“我的朋友。”;参数的长度为“0”;添加图例值,r,shiny,choropleth,r-leaflet,R,Shiny,Choropleth,R Leaflet,我正在开发一个闪亮的应用程序来显示choropleth地图,使用传单和形状文件来添加多边形。外部shiny一切正常,但使用shiny时,我在使用addLegend时遇到了如下问题: mypal <- reactive({ colorBin(palette = "RdYlBu", domain = get("map_fires")[[paste0("qm_", selectedYear())]], bins = 5, r
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)
})
我正在使用paste0(“qm”,selectedYear())
来引用包含年度数据的不同列
我相信问题出在values=~get(“map_fires”)[[paste0(“qm_”),selectedYear()]]
,但我不知道为什么。我已经尝试在addLegend
上指定数据集,并在values=
上放置一个特定列(即,不依赖selectedYear()
的反应值),但没有成功
下面的代码很好地处理了外部闪亮的事物(我一直在使用get(“map_fires”)[[paste0(“qm_u”),2018)]
,在特定年份更改selectedYear()
,只是为了更好地展示我的情况):
mypal%
addProviderTiles(“OpenStreetMap.Mapnik”)%>%
添加多边形(数据=贴图),
笔划=真,权重=1,平滑因子=0.2,填充不透明度=0.3,
fillColor=~mypal(获取(“地图火灾”)[[paste0(“qm_u”,2018)],
highlight=highlightOptions(
重量=5,
color=“#666”,
fillOpacity=0.7,
bringToFront=真),
label=lappy(统计信息标签,HTML),
标签选项=标签选项(
样式=列表(“font-weight”=“normal”,padding=“3px 8px”),
textsize=“15px”,
direction=“auto”))%%>%
addLegend(position=“bottomright”,pal=mypal,
值=获取(“映射火灾”)[[paste0(“qm”,2018)],
不透明度=1)
我遗漏了什么吗?为什么要使用
get(“map\u fires”)
而不仅仅是map\u fires
?如果你改成那样会怎么样?另外,我会研究这一行:fillColor=~mypal()(get(“map_fires”)[[paste0(“qm_”),selectedYear())]],
我在没有get
的情况下遇到了另一个问题,但现在我意识到我真的不需要它。无论如何,删除它并仅保留map_fires[[paste0(“qm_u”,selectedYear())]]
并没有改变问题。至于行fillColor=~mypal()(get(“map_fires”)[[paste0(“qm_u”,selectedYear())]]
,这不是问题,如果我不添加图例,应用程序可以很好地使用它。如果没有你的数据样本来重现这个问题,我将无法进一步研究它。我已经在这里添加了代码和数据样本:非常感谢你,菲尔!成功了!我几乎可以肯定我以前尝试过删除~
,但可能当时我也遇到了其他问题,使代码无法工作。再次感谢你,菲尔!
Warning: Error in if: argument is of length zero
104: addLegend
...
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)