如何添加带有条件行的传单弹出窗口?

How to add a leaflet pop up with conditional lines?

我有这个数据框:

country_groups <- structure(list(country_name = c("Australia", "Brazil", "Canada", 
"China", "China", "China", "China", "China", "China", "China", 
"China", "China", "China", "China", "China", "China", "European Patent Office", 
"European Patent Office", "Germany", "India", "India", "India", 
"India", "India", "India", "Japan", "Japan", "Japan", "Korea [Republic of]", 
"Korea [Republic of]", "Korea [Republic of]", "Korea [Republic of]", 
"Korea [Republic of]", "Romania", "Russian Federation", "Russian Federation", 
"Spain", "Taiwan", "Taiwan", "United Kingdom", "United States", 
"United States", "United States", "United States", "United States", 
"United States", "United States", "United States", "United States", 
"United States", "United States", "United States", "World", "World"
), longitude = c(133.775136, -51.92528, -106.346771, 104.195397, 
104.195397, 104.195397, 104.195397, 104.195397, 104.195397, 104.195397, 
104.195397, 104.195397, 104.195397, 104.195397, 104.195397, 104.195397, 
-20.8685430762787, -20.8685430762787, 10.451526, 78.96288, 78.96288, 
78.96288, 78.96288, 78.96288, 78.96288, 138.252924, 138.252924, 
138.252924, 127.766922, 127.766922, 127.766922, 127.766922, 127.766922, 
24.96676, 105.318756, 105.318756, -3.74922, 120.960515, 120.960515, 
-3.435973, -95.712891, -95.712891, -95.712891, -95.712891, -95.712891, 
-95.712891, -95.712891, -95.712891, -95.712891, -95.712891, -95.712891, 
-95.712891, 71.8853560211639, 71.8853560211639), latitude = c(-25.274398, 
-14.235004, 56.130366, 35.86166, 35.86166, 35.86166, 35.86166, 
35.86166, 35.86166, 35.86166, 35.86166, 35.86166, 35.86166, 35.86166, 
35.86166, 35.86166, 48.2343918029004, 48.2343918029004, 51.165691, 
20.593684, 20.593684, 20.593684, 20.593684, 20.593684, 20.593684, 
36.204824, 36.204824, 36.204824, 35.907757, 35.907757, 35.907757, 
35.907757, 35.907757, 45.943161, 61.52401, 61.52401, 40.463667, 
23.69781, 23.69781, 55.378051, 37.09024, 37.09024, 37.09024, 
37.09024, 37.09024, 37.09024, 37.09024, 37.09024, 37.09024, 37.09024, 
37.09024, 37.09024, -51.681674860461, -51.681674860461), topic = c("Population growth", 
"Education", "Arts", "Sports", "Growing plants", "Reading", "Story telling", 
"Gymnastics", "Cooking classes", "Education", "Arts", "Arcade", 
"Acting", "Population growth", "Movies", "Education", "Sports", 
"Arcade", "Movies", "Sports", "Reading", "Gymnastics", "Cooking classes", 
"Education", "Population growth", "Sports", "Reading", "Gymnastics", 
"Growing plants", "Gymnastics", "Arcade", "Acting", "Movies", 
"Gymnastics", "Sports", "Gymnastics", "Arcade", "Gymnastics", 
"Arcade", "Gymnastics", "Sports", "Growing plants", "Reading", 
"Gymnastics", "Cooking classes", "Education", "Arts", "Arcade", 
"Acting", "Population growth", "Movies", "Education", "Arcade", 
"Movies"), n = c(2L, 1L, 1L, 34L, 31L, 51L, 82L, 63L, 22L, 17L, 
43L, 53L, 34L, 43L, 46L, 22L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 
4L, 3L, 3L, 23L, 1L, 4L, 2L, 2L, 8L, 2L, 3L, 1L, 1L, 1L)), row.names = c(NA, 
-54L), class = "data.frame")

而且我希望用它来执行传单映射,当您单击相应的标记时,它会显示国家名称,然后是下面的每个 TOPIC = NUM​​BER。我度过了一段艰难的时期,因为我看到他们大多使用 paste0paste 并且能够完成工作。这次每个国家都可以有所有主题或只有一个主题,我已经尝试像他们那样完成它 in the official webpage 但没有运气,这看起来很简单但我做不到,有人可能知道怎么做?您可以在下面看到我使用的编码。提前致谢!

library(leaflet)
library(dplyr)
# Maping
leaflet(country_groups) %>% 
  addTiles() %>% 
  setView( lng = 0, lat = 0, zoom = 1) %>% 
  addProviderTiles("Esri.WorldTopoMap") %>%
  addMarkers(~longitude, ~latitude, popup = ~HTML(paste0("<br>", "Country:", country_name,"<br/>",
                                                    country_groups %>%  
                                                      filter(country_name %in% country_name) %>% 
                                                      select(topic,n) )))

这是我期望的那种弹出窗口(中国),但要预先添加国家/地区名称:

country_selec <- country_groups %>% filter(country_name == "China")
country_info <- country_selec[c("topic","n")];names(country_info) <- c("Topic","Count")


这是提供 answear 后的显示方式,如果像@Wimpel 那样显示就完美了

我过去也遇到过这个问题。这是我的解决方案:

首先,创建一个助手 data.frame,其中包含每个国家/地区的值,名为 df.helper
然后,为每个国家/地区创建一个带有标签的列表(作为带有 html-tags 的字符串)。 最后,构建传单,并使用 htmltools 包中的 lapplyHTML 创建标签,以便在弹出窗口中获得漂亮的 HTML 代码。

当然,您可以编辑创建 custom.labels 的 paste0 命令中的字符串以适合您所需的格式。所有 HTML 标签都是允许的,所以去吧 ;-)

#load libraries
library(leaflet);library(htmltools)
#create a helper data.frame with a unique country_name in each row                                                                                                                                                                                                                                            -54L), class = "data.frame")
df.helper <- country_groups[ !duplicated( country_groups$country_name),  ]
#create the custom labels by country
custom.labels <- lapply( seq( nrow( df.helper ) ), function(i) {
   paste0( '<p><b>', df.helper[i, "country_name" ], '</b><br></p><p>',
           paste( country_groups[which(country_groups$country_name == df.helper[i, "country_name"]), "topic"],
                  country_groups[which(country_groups$country_name == df.helper[i, "country_name"]), "n"],
                 sep = " - ", collapse="<br>"), 
           '</p>' ) 
})
#create the leaflet
leaflet(df.helper) %>% 
   addTiles() %>% 
   setView( lng = 0, lat = 0, zoom = 1) %>% 
   addProviderTiles("Esri.WorldTopoMap") %>%
   addMarkers(
      lng = ~longitude, 
      lat = ~latitude, 
      popup = lapply( custom.labels, htmltools::HTML ) 
      )