有没有办法在 tm_bubbles 项目的弹出窗口中制作超链接?

Is there a way to make hyperlinks in the pop-up of a tm_bubbles item?

我正在使用 tmap 包来可视化地图中的某些值。 有谁知道在交互式地图的弹出框中集成超链接的方法吗?

我所做的是使用下面的代码。我确实看到了所需的 URL,但无法单击它。

然后我在字符串中添加了< A HREF="URL">name< / A >。但这并不能使它起作用。

tmap_mode("view")

tm_shape(dataset) +
  tm_bubbles(size = 1, col = "value1", palette = "-RdBu", popup.vars = c("value2","URL"), text = "value3")

如果能帮助我发现这是否可行,我将不胜感激。

{tmap} 的 3.3-1 版(2021 年 3 月)之前,您对 tmap 中的弹出窗口的控制较少。此后世界发生了变化,您可能 认为这个答案已弃用 - 直接使用 {leaflet} 仍然有效,但它不再是使超链接在弹出窗口中工作的唯一方法。

有关纯 {tmap} 工作流程的示例,请参阅我较新的答案。

通过包 leaflet.

直接使用传单库要容易得多

我首先将弹出窗口的文本粘贴在一起作为 HTML,然后在 leaflet::addCircleMarkers() 调用中将其用作弹出参数(波浪号很重要)。

传单中的圆形标记非常像 tmap 气泡,可以调整为看起来比这更精细;为了代码的简洁起见,我专注于主要主题,即交互式地图的弹出气球中的可自定义超链接。

library(dplyr)   # for mutate & pipe
library(tmap)    # for the metro dataset
library(leaflet) # interface to leaflet

data("metro") # from the tmap package

metro %>% 
  mutate(label = paste("marvel at ", name, " and follow <a href = https://whosebug.com/>stack overflow</a>")) %>% 
  leaflet() %>%
  addTiles(group = "OSM") %>%
  addCircleMarkers(popup = ~label)

详细说明我之前的回答——在撰写本文时该回答有效,但不再有效:{tmap} 包已获得抑制 HTML 代码清理的选项最近弹窗(版本 3.3-1,发布于 2021-03-05)。

因此,对于当前 {tmap}可以 在弹出窗口中包含 HTML 代码,我之前写过的文本格式控制就在这里。

对于可能的实现,请考虑以下代码:

library(dplyr)   # for mutate & pipe
library(tmap)    # for the metro dataset

data("metro") # from the tmap package

metro <- metro %>% 
  mutate(label = paste("marvel at ", name, " and follow <a href = https://whosebug.com/>stack overflow</a>"))

tmap_mode("view")

tm_shape(metro) +
  tm_bubbles(size = 1, col = "red", 
             popup.vars = c("label"),
             popup.format = list(html.escape = F)) +
  tm_basemap(leaflet::providers$Stamen.Watercolor)