传单未在动态生成的 R markdown html knitr 中呈现 [英] Leaflet not rendering in dynamically generated R markdown html knitr

查看:8
本文介绍了传单未在动态生成的 R markdown html knitr 中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 R markdown 报告,其中部分和标签集是动态创建的.

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

我有一个问题,即在输出中未生成传单地图,而是在空白处生成.然而,它们确实在 R 工作室中渲染.

I have an issue where the Leaflet maps are not being generated in the output, but rather a blank white space. They do however render in R studio.

这是一张图片,显示了输出当前的外观以及它的外观.

Here is an image showing how the output currently looks, and how it should look.

这是我尝试过的代码:

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

for (estates in filtered_list){
map_data <- x_estates %>% filter(Street_Postcode == estates)

    cat("###", estates, "{.tabset .tabset-fade .tabset-pills}", "
")
    cat("


")
    cat("This is where the map will go ")
    
# generate leaflet plot (doesn't even show white space if not stored in tagList)
    page <- tagList(
leaflet() %>% addTiles() %>% 
  addMarkers(map_data$Longitude, map_data$Latitude)
)

### options i've tried for getting the Leaflet map to plot correctly
   
page
print(page)
print(page[[1]])
page[[1]]
 
print(
 tagList(
     page[[1]]
    ))
 
 print(
    tagList(
     page
    ))
### end 

        cat("


")
    

     for (major in Major$Titles) {
     
         cat("####", major, "
")
         cat("


") 
         
##### 
rest of code
#####

}
}


我认为这与 html 渲染有关,因为我使用 ggmap 工作生成的静态图;但是我希望我的地图是交互式的.

It has something to do with html rendering I presume, as static plots i've generated using ggmap work; however I want my maps to be interactive.

注意地图在 R studio 中渲染得很好,只是在 knitr html 输出中没有.

N.B. The maps render fine in R studio, just not in the knitr html output.

编辑

按照 Susan Switzer 的建议,我对代码进行了一些更改.现在显示传单图(也具有交互性).可以更改代码以动态命名和加载正确的 HTML 文件.

As suggested by Susan Switzer, I have made some changes to the code. The leaflet plots now show (with interactivity too). Can alter the code to dynamically name and load the correct HTML file.

    m <- leaflet() %>% addTiles() %>% 
addMarkers(map_data$Longitude, map_data$Latitude)
library(mapview)
m <- mapshot(m, url = paste0(getwd(), "/map.html"))
# m <- htmltools::includeHTML("map.html") produces an odd output where the entire document becomes a laggy leaflet map
# print(m) 

m <- htmltools::tags$iframe(title = "Map", src = "map.html")
print(m)

编辑 2

Waldi 已经解决了使用积分的问题.然而,在使用集群或热图时也会出现同样的问题.例如

Waldi has solved the issue when using points. However the same problem occurs when using clusters or heatmaps. e.g.

library(tidyverse)
library(leaflet)
library(leaflet.extras)
leaflet()

显示具有 3 组坐标的示例的最少代码

Minimal code to show examples with 3 sets of coordinates

long <- as.numeric(c("0.005638", "0.005648", "0.005658"))
lat <- as.numeric(c("51.62879", "51.62889", "51.62879"))
data1 <- data.frame(long, lat)


filtered_list <- 1:3
cat("## Tabs {.tabset .tabset-fade .tabset-pills}", "
")
for (estates in filtered_list){
    cat("###", estates, "
")
    cat("


")
    cat("This is where the map will go ")
    
        cat("1 ")
# 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=data1$long, lat=data1$lat, popup="The birthplace of R")
    )
    cat(as.character(page))
    
            cat("2 ")
    page <- htmltools::tagList(
         leaflet() %>%
            addTiles() %>%  # Add default OpenStreetMap map tiles
            addMarkers(lng=data1$long, lat=data1$lat, popup="The birthplace of R", clusterOptions = markerClusterOptions()) 
    )
    cat(as.character(page))
    
    
        
            cat("3 ")
    page <- htmltools::tagList(
         leaflet() %>%
            addTiles() %>%  # Add default OpenStreetMap map tiles
           addMarkers(lng=data1$long, lat=data1$lat, popup="The birthplace of R") %>% 
           addHeatmap(
    lng = data1$lat, lat = data1$long,
    blur = 20, max = 5, radius = 40
    )
    )
    cat(as.character(page))
    
    
        
            cat("4 ")
    page <- htmltools::tagList(
         leaflet() %>%
            addTiles() %>%  # Add default OpenStreetMap map tiles
           addMarkers(lng=data1$long, lat=data1$lat, popup="The birthplace of R") %>% 
           addHeatmap(
    lng = data1$lat, lat = data1$long,
    blur = 20, max = 5, radius = 40
    )
    )
    cat(as.character(page))
    
    
    }

推荐答案

这是与 here with Highcharter

试试:

---
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}", "
")
for (estates in filtered_list){
    cat("###", estates, "
")
    cat("


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

这篇关于传单未在动态生成的 R markdown html knitr 中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆