[英]How can I create a label/tooltip based on a list column of a data.frame
I wish to create a constituency map of the UK in leaflet which when hovered over provides constituency name and detailed result. 我希望在传单中创建一个英国的选区地图,当它悬停时提供选区名称和详细结果。
The data is contained in a tibble with 2 columns: 数据包含在包含2列的tibble中 :
I have included a sample of two constituencies below 我在下面列出了两个选区的样本
df <- structure(list(constituency = c("Knowsley", "Bristol West"),
result = list(structure(list(name = c("George Howarth", "James Spencer",
"Neil Miney", "Carl Cashman", "Steve Baines"), party = c("Labour",
"Conservative", "UKIP", "LD", "Green"), votes = c(47351L,
5137L, 1285L, 1189L, 521L), pc = c(85.34, 9.26, 2.32, 2.14,
0.94), order = 1:5), .Names = c("name", "party", "votes",
"pc", "order"), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(name = c("Thangam Debbonaire",
"Annabel Tall", "Molly Scott Cato", "Stephen Williams", "Jodian Rodgers"
), party = c("Labour", "Conservative", "Green", "LD", "Money Free Party"
), votes = c(47213L, 9877L, 9216L, 5201L, 101L), pc = c(65.93,
13.79, 12.87, 7.26, 0.14), order = 1:5), .Names = c("name",
"party", "votes", "pc", "order"), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("constituency", "result"))
If I just wanted the constituency in the label, I would have coded like this that I could apply to the leaflet output: 如果我只是想要标签中的选区,我会像这样编码,我可以应用于传单输出:
labels <- sprintf(
df$constituency
) %>% lapply(htmltools::HTML)
But I wish to add in the result details. 但我希望在结果细节中添加。
How bout this? 怎么回事?
labels = lapply(1:length(df$result), function(i) {
tmp = format(df$result[[i]])
tmp = tmp[3:length(tmp)]
tmp[1] = df$constituency[i]
htmltools::HTML(paste(tmp, collapse = "<br>"))
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.