0

I have a bunch of pre-saved plots inside a folder. I have a Rmarkdown(Flexdashboard) looping this folder and showing a picture per tab this way (an example taking just one)

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

```

this worked flawlessly. My issue is when using a loop. The tabs are rendered but no plot inside. This way:

```{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")
}
```
Edo
  • 7,567
  • 2
  • 9
  • 19
Forge
  • 1,587
  • 1
  • 15
  • 36
  • Does this answer your question? [Insert images using knitr::include\_graphics in a for loop](https://stackoverflow.com/questions/51268623/insert-images-using-knitrinclude-graphics-in-a-for-loop) – Edo Nov 18 '21 at 17:47

1 Answers1

0

Similarly to this, I'll drop here a fully reproducible code for 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))
}
```
Edo
  • 7,567
  • 2
  • 9
  • 19