1

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') 
}
```
user20650
  • 24,654
  • 5
  • 56
  • 91
bob123
  • 153
  • 1
  • 1
  • 9
  • you may need a [`grid::grid.newpage()`](https://stackoverflow.com/questions/34597687/why-does-2nd-ggplot-not-appear-using-knitr-and-grid/34598187#34598187) – user20650 Feb 15 '18 at 23:44
  • Many thanks user20650! Adding grid::grid.newpage() first fixes the problem! – bob123 Feb 16 '18 at 00:52
  • you're welcome - I'll mark it as a duplicate as I think they are more or less the same. – user20650 Feb 16 '18 at 01:18

0 Answers0