在 for 循环中使用 knitr::include_graphics 插入图像
Insert images using knitr::include_graphics in a for loop
```{r}
knitr::include_graphics(path = "~/Desktop/R/Files/apple.jpg/")
```
上面的代码块工作正常。但是,当我创建一个 for
循环时,knitr::include_graphics
似乎不起作用。
```{r}
fruits <- c("apple", "banana", "grape")
for(i in fruits){
knitr::include_graphics(path = paste("~/Desktop/R/Files/", i, ".jpg", sep = ""))
}
```
这是一个已知问题 knitr include_graphics doesn't work in loop #1260。
解决方法是在 for 循环中生成图像路径并 cat
它们。要显示最终结果,需要 result = "asis"
。
```{r, results = "asis"}
fruits <- c("apple", "banana", "grape")
for(i in fruits) {
cat(paste0(""), "\n")
}
```
此处每次迭代都会生成图形的降价路径(例如,""
)
正如一辉所说 include_graphics()
必须在顶级 R 表达式中使用 here
我的解决方法是:
```{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)
```
```{r}
knitr::include_graphics(path = "~/Desktop/R/Files/apple.jpg/")
```
上面的代码块工作正常。但是,当我创建一个 for
循环时,knitr::include_graphics
似乎不起作用。
```{r}
fruits <- c("apple", "banana", "grape")
for(i in fruits){
knitr::include_graphics(path = paste("~/Desktop/R/Files/", i, ".jpg", sep = ""))
}
```
这是一个已知问题 knitr include_graphics doesn't work in loop #1260。
解决方法是在 for 循环中生成图像路径并 cat
它们。要显示最终结果,需要 result = "asis"
。
```{r, results = "asis"}
fruits <- c("apple", "banana", "grape")
for(i in fruits) {
cat(paste0(""), "\n")
}
```
此处每次迭代都会生成图形的降价路径(例如,""
)
include_graphics()
必须在顶级 R 表达式中使用 here
我的解决方法是:
```{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)
```