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
Delete Rows Based on Multiple Conditions with Dplyr
How to Reset All Options() Arguments to Their Default Values
How to Do a Data.Table Rolling Join
Set Environment Variables for System() in R
Ggplot2 Time Series Plotting: How to Omit Periods When There Is No Data Points
Write a Data Frame to CSV File Without Column Header in R
Pretty Axis Labels for Log Scale in Ggplot
Remove Consecutive Duplicates from Dataframe
Porting Set Operations from R's Data Frames to Data Tables: How to Identify Duplicated Rows
How to Convert .Rdata Format into Text File Format
What Is the Fastest Way to Get a Vector of Sorted Unique Values from a Data.Table
With the R Package Xlsx, How to Set Na.Strings When Reading an Excel File
Knitr: How to Use Child .Rnw Docs with (Relative) Figure Paths
Linear Models in R with Different Combinations of Variables
How to Fill in the Contour Fully Using Stat_Contour