如何使用Knitr在RMD中的循环中渲染传单地图 [英] How to render leaflet-maps in loops in RMDs with knitr
问题描述
我目前正在努力尝试使knitr呈现我的传单地图,该传单地图来自一个集合,以正确显示在呈现的RMD html输出中.我已经知道在循环遍历集合并使用RMD/knitr生成图形输出时存在一些潜在的问题,但是我仍然不知道如何使我的示例适用于传单地图.
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 potential problems when looping throug collections and generating graphical output with RMD/knitr, but still I can't figure out, how to make my example work for leaflet-maps.
可复制的工作示例(Test_1.Rmd):
---
title: "test1"
author: "phabee"
date: "22 Mai 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title 1
```{r, fig.show='asis', echo=FALSE, results='asis'}
for (i in 1:4) {
cat("### Plot Number ", i, "\n")
plot(1,1)
# use plot.new() here to force rendering of potential plot-duplicates
plot.new()
cat("\n\n")
}
```
上面的示例呈现了预期的效果(至少在添加plot.new()之后,我学会了
The above example renders as expected (at least after adding plot.new(), which I've learned here from Freedomtowin). But when I try to do the same with leaflet-maps, it doesn't work at all. No single map is being rendered:
可复制的失败示例(Test_2.Rmd)
---
title: "test2"
author: "phabee"
date: "22 Mai 2018"
output: html_document
---
```{r setup, include=FALSE}
library(leaflet)
knitr::opts_chunk$set(echo = TRUE)
```
## Title 1
```{r, fig.show='asis', echo=FALSE, results='asis'}
for (i in 1:4) {
cat("### Map Number ", i, "\n")
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
cat("\n")
}
```
我希望第二个Rmd渲染同一张地图的4倍,显示不同的标题(地块编号1-4").但是输出根本不会渲染任何地图.输出结果如下:
I would expect the second Rmd to render 4 times the same map, showing different titles ("Plot Number 1-4"). But the output doesn't render any map at all. The output looks as follows:
检查生成的html-output后,可以看到根本没有渲染任何东西,而不仅仅是可见性问题:
After inspecting the generated html-output the output, it can be seen that there is nothing rendered at all and it's not just a visibility-issue:
但是,当我直接通过突出显示"代码并按ctrl-Enter来直接评估第二个Rmd中的小叶部分时,地图将按预期方式呈现:
However, when I evaluate the leaflet-section within the 2nd Rmd directly by 'highlighting' the code and hitting ctrl-Enter, the map renders as expected:
我已经尝试过
- 将传单声明转换为赋值语句
- 引入cat()或print()命令以强制地图输出
- 在地图输出部分之前和/或之后使用其他换行符'\ n'
- 在 fig.show 或 results 中使用"asis"指令
- convert the leaflet-statement to an assignment statement
- introduce cat() or print() commands to force map-output
- play around with additional newline-characters '\n' before and/or after the map output section
- fiddle around with the 'asis' directives from fig.show or results
没有任何效果.有人在这里有线索吗?
without any effect. Does anyone have a clue here?
推荐答案
您需要将内容放入tagList,并从块中打印该列表.这仅使用fig.show
和results
的默认设置.它还使用htmltools::h3()
函数将标题直接转换为HTML标题,而不使用Markdown ###
标记. (您可能需要h2
或h4
.)
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)
```
这篇关于如何使用Knitr在RMD中的循环中渲染传单地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!