How to render leaflet-maps in loops in RMDs with knitr

后端 未结 1 1398
孤街浪徒
孤街浪徒 2021-01-21 04:19

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 p

1条回答
  •  余生分开走
    2021-01-21 04:33

    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)
    ```
    

    0 讨论(0)
提交回复
热议问题