如何根据 data.frame 的列表列创建 label/tooltip

How can I create a label/tooltip based on a list column of a data.frame

我希望在传单中创建英国的选区地图,将鼠标悬停在该地图上时会提供选区名称和详细结果。

数据包含在具有 2 列的 tibble 中:

  1. 选区,其中包含名称
  2. result,这是每个单元格中包含 data.frame 的列表列,其中包括 候选人姓名投票%订单

我在下面包含了两个选区的样本

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"))

如果我只想要标签中的选区,我会这样编码,我可以将其应用于传单输出:

labels <- sprintf(
              df$constituency
          ) %>% lapply(htmltools::HTML)

但我想在结果中添加详细信息。

这个怎么样?

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>"))
})