使用knitr :: include_graphics在for循环中插入图像 [英] Insert images using knitr::include_graphics in a for loop

查看:77
本文介绍了使用knitr :: include_graphics在for循环中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

```{r}
knitr::include_graphics(path = "~/Desktop/R/Files/apple.jpg/")
```

上面的代码块工作正常.但是,当我创建一个for循环时,knitr::include_graphics似乎无法正常工作.

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 = ""))
}
```

推荐答案

这是一个已知问题 knitr include_graphics在#1260循环中不起作用.

一种解决方法是在for循环内生成图像的路径,并cat它们.要显示最终结果,需要result = "asis".

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

每次迭代都会在此处生成图形的降价路径(例如,"![](~/Desktop/R/Files/apple.jpg)")

Here each iteration generates markdown path to graphics (eg, "![](~/Desktop/R/Files/apple.jpg)")

这篇关于使用knitr :: include_graphics在for循环中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆