0

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
```
haff
  • 918
  • 2
  • 9
  • 20
  • 1
    Could you create the grid after you add the maps to leaflet? https://stackoverflow.com/questions/45175040/multiple-leaflets-in-a-grid – Susan Switzer Jul 07 '22 at 21:59
  • @SusanSwitzer This approach is a really clever idea and does produce the maps appropriately, but the caption does not appear. – haff Jul 13 '22 at 18:12

0 Answers0