emaoca
emaoca

Reputation: 119

RMarkdown HTML document isn't showing Tmap widget while printing with for cycle

Not able to make RMarkdown html output showing interective Tmap views (tmap_mode set to "view") while printed inside for cycle. Any tips?

This works properly:

```{r interactive maps, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}

library(pander)
library(knitr)

library(tmap)
data("World")

tmap_mode("view")

map_1 <- tm_shape(World) +
  tm_polygons("HPI")

map_2 <- tm_shape(World) +
  tm_polygons("economy")

map_3 <- tm_shape(World) +
  tm_polygons("footprint")

maps <- list(map_1, map_2, map_3)

pandoc.header(("Map n° 1"), level = 3)
maps[[1]]

pandoc.header(("Map n° 2"), level = 3)
maps[[2]]

pandoc.header(("Map n° 3"), level = 3)
maps[[3]]

```

How html document exported from Markdown appers (avoiding use for loop): HTML OUTPUT:

This doesn't... The problem seems for loop doesn't give back properly tmap widgets...

```{r interactive maps from cycle, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}

library(pander)
library(knitr)

library(tmap)
data("World")

tmap_mode("view")

map_1 <- tm_shape(World) +
  tm_polygons("HPI")

map_2 <- tm_shape(World) +
  tm_polygons("economy")

map_3 <- tm_shape(World) +
  tm_polygons("footprint")

maps <- list(map_1, map_2, map_3)

for (i in 1:length(maps)) {

  pandoc.header(paste("Map n°", i), level = 3)
  print(maps[[i]])
  cat("\n")

}


```

How html document exported from Markdown appers (using for loop): HTML OUTPUT:

Upvotes: 0

Views: 419

Answers (1)

Kestutis Vinciunas
Kestutis Vinciunas

Reputation: 63

try this:


       ```{r, results='asis'}
    for ( i in 1:length(maps) ){
      
      pandoc.header(paste("Map n°", i), level = 3)
     
      lflt <- tmap::tmap_leaflet(maps[[i]])
      tmp_file <- glue::glue("tmp_{i}_.html")
      htmlwidgets::saveWidget(lflt, tmp_file )
      print(
        htmltools::tags$iframe(
          src = tmp_file,
          width = '100%',
          height = 1440,
          scrolling = "no",
          seamless = "seamless",
          frameBorder = "0"
        )
      )
            
             cat("\n")
    
    }
    
    ```

Upvotes: 0

Related Questions