Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何添加带有条件行的弹出传单?_R_Popup_R Leaflet - Fatal编程技术网

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

R 如何添加带有条件行的弹出传单?,r,popup,r-leaflet,R,Popup,R Leaflet,我得到了这个数据框: 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", "Europe

我得到了这个数据框:

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")
这是我期待的弹出窗口(中国),但在前面加上了国家名称:

country_selec <- country_groups %>% filter(country_name == "China")
country_info <- country_selec[c("topic","n")];names(country_info) <- c("Topic","Count")
country\u selec%过滤器(country\u name==“中国”)

country_info我过去也曾为此挣扎过。。以下是我的解决方案:

首先,创建一个helper data.frame,其中包含每个国家的值,名为
df.helper

然后,为每个国家创建一个带有标签的列表(作为带有html标记的字符串)。 最后,构建传单,并使用
htmltools
-包中的
lappy
HTML
创建标签,以在弹出窗口中获得良好的HTML代码

当然,您可以在创建
custom.label
的paste0命令中编辑字符串,以适合您所需的格式。所有HTML标记都是允许的,所以请注意;-)

#加载库
图书馆(单张);图书馆(htmltools)
#创建一个helper data.frame,每行有一个唯一的国家/地区名称-54L),class=“data.frame”)
df.helper%
addTiles()%>%
设置视图(lng=0,lat=0,zoom=1)%>%
addProviderTiles(“Esri.WorldTopoMap”)%%>%
添加标记(
lng=经度,
纬度=~纬度,
popup=lappy(custom.labels,htmltools::HTML)
)

这正是我所需要的,但它对我不起作用:(我不知道它应该是什么样子,但我正在向前运行它,
df.helper
正在变成一个只有16行的数据框,每个国家一行,只有一个主题。在
自定义标签中,我可以看到只有澳大利亚给了我一些东西(
人口增长-2
)。其余国家/地区使用强字母键入名称,但应使用
字符(0)-整数(0)
如下。奇怪..我无法用提供的样本数据和我的答案中的代码重现你的结果…你能尝试一次新的R-session吗,只使用
dplyr
传单和
htmltools
-加载的软件包吗?原来是R-session让我很为难。让我的笔记本电脑关机一整晚,结果是一个错误it’它和你的answear一样漂亮。非常感谢!但是当它在我正在开发的闪亮应用程序中时,它一直给我带来同样的问题,知道为什么吗?:(只需要根据对象逐个设置为字符、数字或数据框!再次感谢:)
country_selec <- country_groups %>% filter(country_name == "China")
country_info <- country_selec[c("topic","n")];names(country_info) <- c("Topic","Count")
#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 ) 
      )