6

I am currently fighting trying to get knitr to render my leaflet maps, taken from a collection to appear correctly in a rendered RMD html-output. I'm already aware about some potential problems when looping throug collections and generating graphical output with RMD/knitr, but still I can't figure out, how to make my example work for leaflet-maps.

Reproducible working example (Test_1.Rmd):

---
title: "test1"
author: "phabee"
date: "22 Mai 2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Title 1

```{r, fig.show='asis', echo=FALSE, results='asis'}
for (i in 1:4) {
    cat("### Plot Number ", i, "\n")
    plot(1,1)
    # use plot.new() here to force rendering of potential plot-duplicates
    plot.new()
    cat("\n\n")
}
```

The above example renders as expected (at least after adding plot.new(), which I've learned here from Freedomtowin). But when I try to do the same with leaflet-maps, it doesn't work at all. No single map is being rendered:

Reproducible failing example (Test_2.Rmd)

---
title: "test2"
author: "phabee"
date: "22 Mai 2018"
output: html_document
---

```{r setup, include=FALSE}
library(leaflet)
knitr::opts_chunk$set(echo = TRUE)
```

## Title 1

```{r, fig.show='asis', echo=FALSE, results='asis'}
for (i in 1:4) {
  cat("### Map Number ", i, "\n")
  leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")  
  cat("\n")
}
```

I would expect the second Rmd to render 4 times the same map, showing different titles ("Plot Number 1-4"). But the output doesn't render any map at all. The output looks as follows:

enter image description here

After inspecting the generated html-output the output, it can be seen that there is nothing rendered at all and it's not just a visibility-issue:

enter image description here

However, when I evaluate the leaflet-section within the 2nd Rmd directly by 'highlighting' the code and hitting ctrl-Enter, the map renders as expected:

enter image description here

I already tried to

  • convert the leaflet-statement to an assignment statement
  • introduce cat() or print() commands to force map-output
  • play around with additional newline-characters '\n' before and/or after the map output section
  • fiddle around with the 'asis' directives from fig.show or results

without any effect. Does anyone have a clue here?

zx8754
  • 52,746
  • 12
  • 114
  • 209
phabi
  • 362
  • 3
  • 10
  • I thought about a combination of `saveWidget(m, file = "./m.html")` and `htmltools::includeHTML("./m.html")` but I can't make it work. – Christoph May 22 '18 at 14:31
  • package `widgetframe` could possibly work.. – symbolrush May 22 '18 at 15:05
  • using widgetframe did not work either and fiddling around with saving html seems no option; my current working but superugly workaround is adding explicit sections for the first n iterations to generate the map-output with concrete values of i. – phabi May 24 '18 at 08:57

1 Answers1

4

You need to put things in a tagList, and have that list print from the chunk. This just uses default settings for fig.show and results; it also uses the htmltools::h3() function to turn the title into an HTML title directly, not using the Markdown ### marker. (You might want h2 or h4 instead.)

---
title: "test3"
output: html_document
---

```{r setup, include=FALSE}
library(leaflet)
library(htmltools)
knitr::opts_chunk$set(echo = TRUE)
```

## Title 1

```{r echo=FALSE}
html <- list()
for (i in 1:4) {
  html <- c(html, 
            list(h3(paste0("Map Number ", i)),
                 leaflet() %>%
                 addTiles() %>%  # Add default OpenStreetMap map tiles
                 addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")  
                 )
            )
}
tagList(html)
```
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • I have some similar problems. Perhaps you know, what's going on? See [Q1](https://stackoverflow.com/q/56989205/5784831), [Q2](https://stackoverflow.com/q/56986638/5784831) and [Q3](https://stackoverflow.com/q/56983105/5784831). I am working on the Rmarkdown level (preferred). – Christoph Jul 11 '19 at 12:33