【问题标题】:How can I create a label/tooltip based on a list column of a data.frame如何根据 data.frame 的列表列创建标签/工具提示
【发布时间】:2017-06-24 15:41:43
【问题描述】:

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

数据包含在具有 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)

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

【问题讨论】:

    标签: r leaflet tibble


    【解决方案1】:

    这个怎么样?

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

    【讨论】:

      猜你喜欢
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 2023-02-14
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多