1

I'm using the tmap package to visualise some values in a map. Does anyone know a way to integrate hyperlinks in the popup box of an interactive map?

What I did was use the code below. I do see the desired URL, but one can't click it.

Then I added < A HREF="URL">name< / A > to the string. But that doesn't make it work.

tmap_mode("view")

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

I would appreciate any help in discovering if this is possible at all.

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
Bart Kers
  • 13
  • 2
  • This answer https://stackoverflow.com/a/69640724/13478749 describes controlling `tmap` popups, including using HTML and URL links. – hugh-allan Oct 21 '21 at 01:14

2 Answers2

1

Prior to release 3.3-1 of {tmap} (March 2021) you had less control over popups in tmap. The world has changed since, and you may consider this answer deprecated - using {leaflet} directly still works, but it is no longer the only way to make hyperlinks work in popups.

For an example of a purely {tmap} workflow please see my newer answer.

It is much easier to utilize leaflet library directly, via package leaflet.

I first paste together the text of the popup as HTML and then use it in leaflet::addCircleMarkers() calls as popup argument (the tilde is important).

The circle markers from leaflet are very much like tmap bubbles, and can be tuned to look much finer than this; for the sake of brevity of code I have concentrated on the main topic, i.e. customizable hyperlinks in popup balloons of an interactive map.

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://stackoverflow.com/>stack overflow</a>")) %>% 
  leaflet() %>%
  addTiles(group = "OSM") %>%
  addCircleMarkers(popup = ~label)

screenshot

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • 1
    Thanks so much for your superfast reply. I can definitely work with this! – Bart Kers Apr 17 '19 at 06:40
  • Regarding "_As far as I know you have little control over popups in tmap_" - the following answer describes how to control popups in `tmap`: https://stackoverflow.com/a/42180837/13478749. Aside from including links which don't automatically link to a URL in tmap, I would say it's just as easy to control plain text popups in `tmap` than leaflet. – hugh-allan Oct 20 '21 at 01:21
  • @hugh-allan this is an oldish answer, and no longer fully valid - at the time of its writing {tmap} did not have the html.escape argument in popup.format, and all HTML (including links) was sanitized, with no recourse. The option was introduced in v3.3-1, released earlier this year (March 2021) – Jindra Lacko Oct 20 '21 at 07:18
  • 1
    Yep, my apologies - I didn't mean to discredit your comment at all, just thought I'd provide an answer to it, for anyone looking to do this now or in the future. Especially as this seems to be one of the first threads that comes up when searching this topic. Nice new answer! – hugh-allan Oct 21 '21 at 01:10
  • 1
    @hugh-allan no worries, I was only half serious :) And thanks for pointing out this oldish question to me - for if you did not I would let the old answer stand, and would not include the updated one. Which would be a shame, as the good folks behind `{tmap}` put so much time & effort to improve the package. – Jindra Lacko Oct 21 '21 at 05:46
1

To elaborate a bit on my earlier answer - which was valid at the time of writing, but is no longer: the {tmap} package has gained option to suppress the sanitization of HTML code in popups recently (in version 3.3-1, released on 2021-03-05).

So with current {tmap} it is possible to include HTML code in popups, and the control over text formatting I wrote about earlier is here.

For a possible implementation consider this code:

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://stackoverflow.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)

world map with leaflet popups containing HTML link

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44