I am using the tmap package (v. 3.0 and I also tried v. 3.2) in R to make an HTML map. I have a spatial polygon of both numeric and character text fields. In a few of my text fields, I have a list of items, which are semi-colon (;) separated. For some of these polygons, the text strings are so long that the popups can be wider than the HTML window. I have to zoom out of the map in order to see the contents of these popups.
I tried to incorporate breaks in these text strings to make new lines within that data field for the popup, but tmap does not recognize them. Such breaks as \n are not creating new lines in the popups.
Below is an example of trying to add different kinds of breaks to a column called long_names and its output:
# install latest version of `tmap` and `tmaptools`
# library(remotes)
# install_github("mtennekes/tmaptools")
# install_github("mtennekes/tmap")
# load libraries
library(sf)
library(raster)
library(spData)
library(tmaptools)
library(tmap)
# use spData dataset called nz
# add example row with long list of names
nz$long_names <- 'long name 1; long name 2; long name 3; long name 4; long name 5'
# try to make multiple lines in string for the popups
# try different codes for breaks
nz$long_names_n <- gsub("; ", "\n", nz$long_names)
nz$long_names_r <- gsub("; ", "\r", nz$long_names)
nz$long_names_br1 <- gsub("; ", "<br/>", nz$long_names)
nz$long_names_br2 <- gsub("; ", "<br>", nz$long_names)
# change tmap to view mode
tmap_mode("view")
# plot interactive map
tm_shape(nz) +
tm_fill()
Output screenshot: long string in HTML popup and it doesn't wrap
In my real dataset, I have differing amounts of items listed within long_names, so I am hoping to have popups of different lengths (and up and down scrolling is fine) but similar widths.
I also tried using tmap_leaflet() with popupOptions() to adjust the maximum popup width, but it ignores it and gives me the same map as before:
# interactive map
map1 <- tm_shape(nz) +
tm_fill()
# convert to leaflet object
lf <- tmap_leaflet(map1)
# assign minimum and maximum width to popup options
lf <- lf %>% leaflet::popupOptions(minWidth = 50,
maxWidth = 50)
# view leaflet version
lf
Output:
$maxWidth [1] 50 $minWidth [1] 50 $maxHeight $autoPan [1] TRUE $keepInView [1] FALSE $closeButton [1] TRUE $className [1] ""
Screenshot: same result as before with popup being too wide
I also tried popup.format = list(), but I think it's not working because the data are not numeric (see here under popup.format). Please correct me if I am wrong about that. Under legend.format of that same link, it says I can put in functions, but I do not know how to make that work these semi-colon-separated strings.
# plot interactive map
tm_shape(nz) +
tm_fill(popup.format = list(long_names=list(maxWidth=50)))
Error:
Error in formatC(x = c(12500.5611491, 4941.5725565, 23900.0363831, 12071.1446886, : unused argument (maxWidth = 50)
My key questions:
- Is there a way to wrap text in popups using
tmapor havetmaprecognize new line breaks within strings? - If not, how can I use
tm_leaflet()and theleafletto modifytmappopup options? My real data set has 30 fields within the polygon, and 6 data fields with this problem--including varying numbers of items in each list--so I'd like to do this without having to enter all 30 data fields individually, if possible (see here). - Or, is it possible to include left to right scroll bars within the popups, instead?
Thanks so much for your help!






