我正在尝试实现purrr
的{{1}}函数以将图层添加到map()
映射中,但是却在如何减少结果上苦苦挣扎。
运行此代码时,我得到一个列表,其中每个项目都是单独的传单地图(正确生成)。通常,从现有地图开始并使用leaflet
函数将图层添加到传单地图。
很抱歉没有进行reprex,但是数据太大/太复杂了,无法在此处包含。
有人对如何组合/减少最终作为列表中的单独地图生成的图层有建议吗?
add
运行layerlst <- lst(all_births = tibble(map = T,
group = "All Births",
pal = "Greens",
datacol = "births_all_births"),
birth_rate = tibble(map = T,
group = "Birth Rate",
pal = "Greens",
datacol = "birth_rate"))
mymap <- leaflet()
mymap <- layerlst %>%
purrr::map(function(layer) {
palette <- colorNumeric(
palette = layer$pal,
domain = range(select(cdc, !!layer$datacol), na.rm = T)
)
mymap %>%
addProviderTiles("CartoDB.Positron", group = layer$group) %>%
addPolygons(data = cdc_geo,
fillColor = ~palette(cdc_geo@data %>% pull(!!layer$datacol)),
color = "#b2aeae",
fillOpacity = 0.7,
weight = 1,
smoothFactor = 0.2,
popup = popup,
group = layer$group) %>%
addLegend(pal = palette,
values = cdc_geo@data %>% pull(!!layer$datacol),
#labFormat = labelFormat(transform = function(x) 100*x, suffix = "%"),
position = "bottomleft",
title = layer$group)
}
)
和mymap[1]
以查看结果正确产生了每一层。