I'm using tmap to produce maps within an interactive RMarkdown report. In order to be able to utilize figure captions and figure numbers, I figured out that I need to create a leaflet object from the tmap map. This is easy enough:
library(tmap)
tmap_mode("view")
data(World)
w1 <- World[1,]
w1_map <- tm_shape(w1) +
tm_polygons()
tmap_leaflet(w1_map)
But if I try to place two maps side-by-side and pass this to the tmap_leaflet function, I get an error. I should note that using tmap_arrange works on its own.
library(tmap)
data(World)
w1 <- World[1,]
w2 <- World[2,]
tmap_mode("view")
w1_map <- tm_shape(w1) +
tm_polygons()
w2_map <- tm_shape(w2) +
tm_polygons()
two_maps <- tmap_arrange(w1_map, w2_map)
two_maps
tmap_leaflet(two_maps)
Error:
Error in if (!is.na(g$style)) { : argument is of length zero
Am I doing something wrong or is this a limitation with tmap that tmap_arrange objects can't be passed to tmap_leaflet?
EDIT
The link referenced in the comment from @Susan Switzer got me a little closer, but the caption does not appear using the "tagList" approach of htmltools:
```{r side-by-side, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, fig.cap = "Some maps"}
library(tmap)
library(htmltools)
data(World)
w1 <- World[1,]
w2 <- World[2,]
tmap_mode("view")
w1_map <- tm_shape(w1) +
tm_polygons()
w2_map <- tm_shape(w2) +
tm_polygons()
w1_map_lf <- tmap_leaflet(w1_map)
w2_map_lf <- tmap_leaflet(w2_map)
leaflet_grid <-
tagList(
tags$figure(
tags$table(width = "100%",
tags$tr(
tags$td(w1_map_lf),
tags$td(w2_map_lf)
)
)
)
)
leaflet_grid
```