gpt4 book ai didi

r - 传单未在动态生成的 R markdown html knitr 中呈现

转载 作者:行者123 更新时间:2023-12-05 06:12:54 25 4
gpt4 key购买 nike

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

我有一个问题,即在输出中没有生成 Leaflet map ,而是一个空白区域。然而,它们确实在 R studio 中呈现。

这是一张图片,显示了输出当前的样子,以及它应该看起来的样子。

Text

这是我试过的代码:

{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
#####

}
}


我认为它与 html 渲染有关,因为我使用 ggmap 工作生成了静态图;但是我希望我的 map 具有交互性。

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

编辑

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

Text

    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 解决了使用积分的问题。但是,使用集群或热图时会出现同样的问题。例如

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

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

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


}

最佳答案

这与描述的问题类似 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}", "\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))
}
```

enter image description here

关于r - 传单未在动态生成的 R markdown html knitr 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63495178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com