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

Flo*_*Flo 4 r rstudio knitr r-markdown

我对即将推出的 RStudio 笔记本感到非常兴奋(在RStudio预览版中可用。有关简短概述,请单击此处)。但是,我在包含图像时遇到了一些困难

在 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')
```
Run Code Online (Sandbox Code Playgroud)

RStudio 标志

但是,当我想在笔记本中执行相同操作时(注意从html_document到的更改html_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')
```
Run Code Online (Sandbox Code Playgroud)

这里应该有一个标志

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

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

Please note: I want to use r-code to include the image. I do not want to include images with standard markdown (![image description](path/to/image)), which works in both the notebook and the regular rmarkdown document. I also do not want to use html. My hope would be that using r-code to include the image would render the image in the notebook.

Edit: One difference between regular Rmarkdown files and notebooks is that notebooks are "previewed" and not knit:

笔记本的预览按钮

Jon*_*han 6

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

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)
Run Code Online (Sandbox Code Playgroud)

不过,这有点小技巧,所以我们只是添加了对knitr::include_graphics. 它将在明天的每日(0.99.1272 或之后)。