如何根据data.frame

时间:2017-06-24 15:41:44

标签: r leaflet tibble

我希望在传单中创建一个英国的选区地图,当它悬停时会提供选区名称和详细结果。

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

  1. 选区,其中包含名称
  2. 结果,这是包含data.frame的每个单元格中的列表列,包括候选人名称派对投票订单
  3. 我在下面列出了两个选区的样本

    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)
    

    但我希望添加结果详细信息。

1 个答案:

答案 0 :(得分:0)

这是怎么回事?

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