Leaflet not rendering in dynamically generated R markdown html knitr

后端 未结 2 1385
说谎
说谎 2021-01-16 16:23

I\'ve created a R markdown report where the sections and tabsets are dynamically created.

I have an issue where the Leaflet maps are not being generated in the output

相关标签:
2条回答
  • 2021-01-16 17:07

    This is a similar problem as described here with Highcharter

    Try:

    ---
    title: "Test Leaflet Tabs"
    output: html_document
    ---
    
    `r knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, cache = F)`
    
    ```{r setup, include=FALSE}
    library(leaflet)
    leaflet()
    ```
    
    ```{r,results='asis'}
    
    filtered_list <- 1:3
    cat("## Tabs {.tabset .tabset-fade .tabset-pills}", "\n")
    for (estates in filtered_list){
        cat("###", estates, "\n")
        cat("\n\n\n")
        cat("This is where the map will go ")
        
    # generate leaflet plot (doesn't even show white space if not stored in tagList)
        page <- htmltools::tagList(
             leaflet() %>%
                addTiles() %>%  # Add default OpenStreetMap map tiles
                addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
        )
        cat(as.character(page))
        }
    ```
    
    

    0 讨论(0)
  • 2021-01-16 17:12

    I had a similar issue that was solved by putting the leaflet into a widget frame.

    l <- leaflet() %>% 
      addTiles() %>% 
      addMarkers(map_data$Longitude, map_data$Latitude)
    
    widgetframe::frameWidget(l, width = "100%")
    

    This has the effect of saving html tiles for use in rendering. I'm pretty sure it just wraps the leaflet in a HMTL tag.

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