使用 knitr::include_graphics() 在 RStudio 笔记本中包含图像

Include images in RStudio notebook with knitr::include_graphics()

我对即将推出的 RStudio 笔记本(在 preview version of RStudio. For a short overview click here 中可用)感到非常兴奋。但是,我在包含图像 时遇到了一些困难。

在 Rmarkdown 中,我可以包含这样的图片:

--- 
title: "This works"
output: html_document
---

```{r echo=FALSE, out.width='10%'}
library(knitr)
knitr::include_graphics('https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png')
```

然而,当我想在笔记本中做同样的事情时(注意从 html_documenthtml_notebook 的变化),我不再得到图像:

--- 
title: "This does not work"
output: html_notebook
---

```{r echo=FALSE, out.width='10%'}
knitr::include_graphics('https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png')
```

(当我使用与笔记本相同的文件夹中的图像时,我只是获取该图像的名称,就像外部图像的 link 一样)。

我的问题:有没有办法在 rmarkdown 代码块 内的笔记本 (更新:) 中也显示图像?

请注意:我想使用 r-code 来包含图像。我不想包含带有标准 markdown (![image description](path/to/image)) 的图像,它在笔记本和常规 rmarkdown 文档中都有效。我也不想使用 html。我希望使用 r 代码来包含图像会在笔记本中呈现图像。

编辑:常规 Rmarkdown 文件和笔记本之间的一个区别是笔记本是 "previewed" 而不是编织:

感谢您抽出宝贵时间试用这些笔记本。今天有一种迂回的方法可以做到这一点;只需绘制一个图并在其上绘制图像即可。

download.file("https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png", "RStudio-Ball.png")
library("png")
ball <- readPNG("RStudio-Ball.png", native = TRUE)
plot(0:1, 0:1, type = "n", ann = FALSE, axes = FALSE)
rasterImage(ball, 0, 0, 1, 1)

虽然有点乱,所以我们刚刚添加了对 knitr::include_graphics 的支持。它将出现在明天的日报中(0.99.1272 或更高版本)。