Insert Images Using Knitr::Include_Graphics in a for Loop

Insert images using knitr::include_graphics in a for loop

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

Inserting images from url to Rmakdown using the knitr::include_graphics

For me the dpi argument of knitr::include_graphics() also doesn't change the size of the image for HTML or DOCX output. What does work though issetting the chunk-option out.width. You can use pixel values (see below) or percent (e.g. "25%").

```{r out.width="300px"}
url <- "https://images.unsplash.com/photo-1563204996-8965f0a4a860?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=934&q=80"
knitr::include_graphics(url)

```

See chapter 5.4 of the R-Markdown Cookbook for more details.

looping in knitr through captions and graphs

Is this a-OK?

```{r, results='asis'}
cat(sprintf('![%s](%s)', df$caption, df$png_file), sep = '\n')
```

Insert multiple images in for-loop into Sweave Document

From the Sweave Manual:

A.7 Creating several figures from one figure chunk does not work

Either save the plots manually and insert them using LaTeX includes (as recommended by the Sweave manual) or switch to knitr. I would recommend the latter.



Related Topics



Leave a reply



Submit