【问题标题】:render images in a loop inside Rmarkdown chunk在 Rmarkdown 块内循环渲染图像
【发布时间】:2021-12-29 13:55:35
【问题描述】:

我在一个文件夹中有一堆预先保存的图。 我有一个 Rmarkdown(Flexdashboard) 循环这个文件夹并以这种方式在每个选项卡上显示一张图片(一个例子只取一个)

```{r ,results="asis"}

list_plots <- list.files(plots_folder,pattern = ".png", full.names = TRUE)


  cat("  \n###",  "Tab Name  \n")
  knitr::include_graphics(list_plots[1])
  cat("  \n")

```

这完美无缺。我的问题是使用循环时。选项卡已渲染,但内部没有绘图。这样:

```{r ,results="asis"}
list_plots <- list.files(plots_folder,pattern = ".png", full.names = TRUE)

for(i in plots) {
  cat("  \n###",  "tab name  \n")
  knitr::include_graphics(i)
  cat("  \n")
}
```

【问题讨论】:

标签: r r-markdown flexdashboard


【解决方案1】:

this 类似,我将在此处放置flexdashboard 的完全可重现的代码:

---
title: "Test"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
```

```{r create plots}
# reproducible example
tmp <- tempdir()
x <- lapply(5:10, function(x){
  png(file.path(tmp, paste0(x,".png")))
  plot(seq(x))
  dev.off()
})
```


```{r, results='asis'}
list_plots <- list.files(tmp, pattern = ".png", full.names = TRUE)

for(i in list_plots) {
  cat(sprintf("\n### tab name\n![](%s)\n", i))
}
```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2021-02-01
    相关资源
    最近更新 更多