如何以编程方式创建传单图标 class 的对象列表?
How can I programmatically create a list of objects of the leaflet icon class?
如何以编程方式创建传单图标的对象列表 class?
我使用 leaflet
库创建了一张地图,最终将显示数十个位置。为此,我想使用 addMarkers
函数添加一些自定义图标,该函数使用 iconList
创建的 iconSet
,如 here 所述。
在下面的示例中(使用来自 https://icon-library.net/ 的图标),通过 iconList
创建 myicons
,其中包含对 makeIcon
的两次直接调用,没有问题,因为只使用了两个图标。然而,在现实世界中,图标的数量、它们的 URL 和其他属性不会事先知道。
如果使用 iconList
创建列表并使用 cbind
将其作为新列附加到数据框,我会收到预期的 "cannot coerce class" 错误消息。
我唯一的选择似乎是以编程方式创建 myicons
列表,但使用 mynewicons <- iconList(sapply(1:nrow(df.data), function(i) {makeIcon(df.data$url[i],iconWidth = df.data$width[i],iconHeight = df.data$height[i])}))
之类的东西会导致 Arguments passed to iconList() must be icon objects returned from makeIcon()
错误。
如何动态创建此传单图标列表而不是提前指定?
require(leaflet)
require(magrittr)
entrynames <- c("Entry 1","Entry 2")
lat <- c(51.509950,51.510736)
lng <- c(-0.1345093,-0.135190)
iconurl <- c("https://icon-library.net/images/right-arrow-icon-png/right-arrow-icon-png-9.jpg",
"https://icon-library.net/images/back_previous_arrow_play_next_stop_pause_101040.png")
iconwidth <- c(60,50)
iconheight <- c(60,50)
df.data <- data.frame(entrynames=entrynames,lat=lat,lng=lng,
url=iconurl,width=iconwidth,height=iconheight,stringsAsFactors = FALSE)
df.data$entrynames <- as.character(df.data$entrynames)
myicons <- iconList(
marker1 = makeIcon(iconUrl = df.data$url[1],iconWidth = df.data$width[1],iconHeight = df.data$height[1]),
marker2 = makeIcon(iconUrl = df.data$url[2],iconWidth = df.data$width[2],iconHeight = df.data$height[2])
)
m <- leaflet() %>% setView(lng = -0.1345093, lat = 51.510090, zoom = 18) %>% addTiles() %>%
addMarkers(data = df.data,
lat = ~lat,
lng = ~lng,
icon = myicons)
m
MRE输出:
有点老套,我不太熟悉传单,但使用 purrr::map
和 purrr::flatten
加上修复名称和属性似乎可行:
require(leaflet)
#> Indlæser krævet pakke: leaflet
require(magrittr)
#> Indlæser krævet pakke: magrittr
entrynames <- c("Entry 1","Entry 2")
lat <- c(51.509950,51.510736)
lng <- c(-0.1345093,-0.135190)
iconurl <- c("https://icon-library.net/images/right-arrow-icon-png/right-arrow-icon-png-9.jpg",
"https://icon-library.net/images/back_previous_arrow_play_next_stop_pause_101040.png")
iconwidth <- c(60,50)
iconheight <- c(60,50)
df.data <- data.frame(entrynames=entrynames,lat=lat,lng=lng,
url=iconurl,width=iconwidth,height=iconheight,stringsAsFactors = FALSE)
df.data$entrynames <- as.character(df.data$entrynames)
myicons <- iconList(
marker1 = makeIcon(iconUrl = df.data$url[1],iconWidth = df.data$width[1],iconHeight = df.data$height[1]),
marker2 = makeIcon(iconUrl = df.data$url[2],iconWidth = df.data$width[2],iconHeight = df.data$height[2])
)
mynewicons <- purrr::map(1:nrow(df.data),
function(i) {
iconList(makeIcon(df.data$url[i],
iconWidth = df.data$width[i],
iconHeight = df.data$height[i])
)
}
) %>%
purrr::flatten()
names(mynewicons) <- glue::glue("marker{1:nrow(df.data)}")
attr(mynewicons, "class") <- "leaflet_icon_set"
identical(myicons, mynewicons)
#> [1] TRUE
由 reprex package (v0.3.0)
于 2019-10-24 创建
如何以编程方式创建传单图标的对象列表 class?
我使用 leaflet
库创建了一张地图,最终将显示数十个位置。为此,我想使用 addMarkers
函数添加一些自定义图标,该函数使用 iconList
创建的 iconSet
,如 here 所述。
在下面的示例中(使用来自 https://icon-library.net/ 的图标),通过 iconList
创建 myicons
,其中包含对 makeIcon
的两次直接调用,没有问题,因为只使用了两个图标。然而,在现实世界中,图标的数量、它们的 URL 和其他属性不会事先知道。
如果使用 iconList
创建列表并使用 cbind
将其作为新列附加到数据框,我会收到预期的 "cannot coerce class" 错误消息。
我唯一的选择似乎是以编程方式创建 myicons
列表,但使用 mynewicons <- iconList(sapply(1:nrow(df.data), function(i) {makeIcon(df.data$url[i],iconWidth = df.data$width[i],iconHeight = df.data$height[i])}))
之类的东西会导致 Arguments passed to iconList() must be icon objects returned from makeIcon()
错误。
如何动态创建此传单图标列表而不是提前指定?
require(leaflet)
require(magrittr)
entrynames <- c("Entry 1","Entry 2")
lat <- c(51.509950,51.510736)
lng <- c(-0.1345093,-0.135190)
iconurl <- c("https://icon-library.net/images/right-arrow-icon-png/right-arrow-icon-png-9.jpg",
"https://icon-library.net/images/back_previous_arrow_play_next_stop_pause_101040.png")
iconwidth <- c(60,50)
iconheight <- c(60,50)
df.data <- data.frame(entrynames=entrynames,lat=lat,lng=lng,
url=iconurl,width=iconwidth,height=iconheight,stringsAsFactors = FALSE)
df.data$entrynames <- as.character(df.data$entrynames)
myicons <- iconList(
marker1 = makeIcon(iconUrl = df.data$url[1],iconWidth = df.data$width[1],iconHeight = df.data$height[1]),
marker2 = makeIcon(iconUrl = df.data$url[2],iconWidth = df.data$width[2],iconHeight = df.data$height[2])
)
m <- leaflet() %>% setView(lng = -0.1345093, lat = 51.510090, zoom = 18) %>% addTiles() %>%
addMarkers(data = df.data,
lat = ~lat,
lng = ~lng,
icon = myicons)
m
MRE输出:
有点老套,我不太熟悉传单,但使用 purrr::map
和 purrr::flatten
加上修复名称和属性似乎可行:
require(leaflet)
#> Indlæser krævet pakke: leaflet
require(magrittr)
#> Indlæser krævet pakke: magrittr
entrynames <- c("Entry 1","Entry 2")
lat <- c(51.509950,51.510736)
lng <- c(-0.1345093,-0.135190)
iconurl <- c("https://icon-library.net/images/right-arrow-icon-png/right-arrow-icon-png-9.jpg",
"https://icon-library.net/images/back_previous_arrow_play_next_stop_pause_101040.png")
iconwidth <- c(60,50)
iconheight <- c(60,50)
df.data <- data.frame(entrynames=entrynames,lat=lat,lng=lng,
url=iconurl,width=iconwidth,height=iconheight,stringsAsFactors = FALSE)
df.data$entrynames <- as.character(df.data$entrynames)
myicons <- iconList(
marker1 = makeIcon(iconUrl = df.data$url[1],iconWidth = df.data$width[1],iconHeight = df.data$height[1]),
marker2 = makeIcon(iconUrl = df.data$url[2],iconWidth = df.data$width[2],iconHeight = df.data$height[2])
)
mynewicons <- purrr::map(1:nrow(df.data),
function(i) {
iconList(makeIcon(df.data$url[i],
iconWidth = df.data$width[i],
iconHeight = df.data$height[i])
)
}
) %>%
purrr::flatten()
names(mynewicons) <- glue::glue("marker{1:nrow(df.data)}")
attr(mynewicons, "class") <- "leaflet_icon_set"
identical(myicons, mynewicons)
#> [1] TRUE
由 reprex package (v0.3.0)
于 2019-10-24 创建