I have a varying number of png files. Within Rmarkdown, I am attempting to create a new heading and display the png file.
I have created a minimal example below.
I use looping construct shown below often in various other similar plotting cases, however with grid.raster I only get the last plot.
Looking for help to resolve this.
---
title: "Buggy Looping with grid.raster"
author: "Author"
output: html_document
---
```{r}
png("test1.png") ; heatmap(as.matrix(iris[,-5])); dev.off()
png("test2.png") ; heatmap(as.matrix(mtcars)) ; dev.off()
pltNames <- list.files(pattern = "^test.+\\.png$")
```
```{r, results='asis'}
for (i in seq_len(length(pltNames))){
cat('\n')
cat("# This is a heading for ", pltNames[i], "\n")
cat('\n')
grid::grid.raster(png::readPNG(pltNames[i]))
cat('\n')
}
```