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

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

问题描述

我已经创建了一个R降价报告,其中的节和标签集是动态创建的.

我有一个问题,其中在输出中未生成传单地图,而是空白.但是它们确实是在R Studio中渲染的.

这是显示输出当前外观和外观的图像.

这是我尝试过的代码:

  {r,echo = FALSE,结果="asis",警告= FALSE,消息= FALSE}的(filtered_list中的属性){map_data<-x_estates%>%过滤器(Street_Postcode == estates)cat("###",遗产,"{.tabset.tabset-fade .tabset-pills}","\ n")cat("\ n \ n \ n")cat(这是地图要去的地方")#生成传单图(如果未存储在tagList中,甚至不显示空格)页面 <- tagList(Leaflet()%>%addTiles()%&%;%addMarkers(map_data $ Longitude,map_data $ Latitude))我尝试过的###选项可以使传单地图正确绘制页打印(页)打印(页面[[1]])页[[1]]打印(标签列表(页[[1]]))打印(tagList(页))### 结尾cat("\ n \ n \ n")的(major $ Titles中的major){cat("####",专业,"\ n")cat("\ n \ n \ n")#####其余代码#####}} 

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

这些地图可以在R Studio中很好地渲染,只是不能在knitr html输出中.

编辑

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

  m<-leaflet()%>%addTiles()%&%addMarkers(map_data $ Longitude,map_data $ Latitude)图书馆(mapview)m<-mapshot(m,url = paste0(getwd(),"/map.html")))#m<-htmltools :: includeHTML("map.html")产生奇数输出,其中整个文档变成了缓慢的传单地图#打印(米)m<-htmltools :: tags $ iframe(title ="map",src ="map.html")印刷(米) 

编辑2

Waldi 已经解决了使用积分时的问题.但是,使用群集或热图时会发生相同的问题.例如

 库(tidyverse)图书馆(传单)图书馆(传单.extras)传单() 

用于显示带有三组坐标的示例的最小代码

  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)过滤列表 <- 1:3cat("##制表符{.tabset.tabset-fade .tabset-pills}","\ n")的(filtered_list中的属性){cat("###",遗产,"\ n")cat("\ n \ n \ n")cat(这是地图要去的地方")猫("1")#生成传单图(如果未存储在tagList中,甚至不显示空格)页面<-htmltools :: tagList(传单()%>%addTiles()%>%#添加默认的OpenStreetMap地图图块addMarkers(lng = data1 $ long,lat = data1 $ lat,popup ="R的出生地"))cat(as.character(page))猫("2")页面<-htmltools :: tagList(传单()%>%addTiles()%>%#添加默认的OpenStreetMap地图图块addMarkers(lng = data1 $ long,lat = data1 $ lat,popup ="R的出生地",clusterOptions = markerClusterOptions()))cat(as.character(page))猫("3")页面<-htmltools :: tagList(传单()%>%addTiles()%>%#添加默认的OpenStreetMap地图图块addMarkers(lng = data1 $ long,lat = data1 $ lat,popup ="R的出生地")%&%addHeatmap(lng = data1 $ lat,lat = data1 $ long,模糊= 20,最大= 5,半径= 40))cat(as.character(page))猫("4")页面<-htmltools :: tagList(传单()%>%addTiles()%>%#添加默认的OpenStreetMap地图图块addMarkers(lng = data1 $ long,lat = data1 $ lat,popup ="R的出生地")%&%addHeatmap(lng = data1$lat, lat = data1$long,模糊= 20,最大= 5,半径= 40))cat(as.character(page))} 

解决方案

这是与

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, 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.

Here is the code i've tried:

{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}", "\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 <- 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("\n\n\n")
    

     for (major in Major$Titles) {
     
         cat("####", major, "\n")
         cat("\n\n\n") 
         
##### 
rest of code
#####

}
}


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.

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

EDIT

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)

EDIT 2

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

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

解决方案

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

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

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