```{r}
knitr::include_graphics(path = "~/Desktop/R/Files/apple.jpg/")
```
The above code chunk works fine. However, when I create a for
loop, knitr::include_graphics
does not appear to be working.
```{r}
fruits <- c("apple", "banana", "grape")
for(i in fruits){
knitr::include_graphics(path = paste("~/Desktop/R/Files/", i, ".jpg", sep = ""))
}
```
This is a known issue knitr include_graphics doesn't work in loop #1260.
A workaround is to generate paths to images within a for loop and cat
them. To show final result result = "asis"
is required.
```{r, results = "asis"}
fruits <- c("apple", "banana", "grape")
for(i in fruits) {
cat(paste0("![](", "~/Desktop/R/Files/", i, ".jpg)"), "\n")
}
```
Here each iteration generates markdown path to graphics (eg, "![](~/Desktop/R/Files/apple.jpg)"
)
include_graphics()
has to be used in top-level R expressions as Yihui stated here
My workaround is this:
```{r out.width = "90%", echo=FALSE, fig.align='center'}
files <- list.files(path = paste0('../', myImgPath),
pattern = "^IMG(.*).jpg$",
full.names = TRUE)
knitr::include_graphics(files)
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With