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

标签 r rstudio knitr r-markdown

我对即将推出的 RStudio 笔记本感到非常兴奋(可在 preview version of RStudio 中找到。有关简短概述,请单击 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')
```

RStudio Logo

但是,当我想在笔记本中做同样的事情时(注意从 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')
```

There should be a Logo here

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

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

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

编辑 :常规 Rmarkdown 文件和笔记本之间的一个区别是笔记本是“预览”而不是编织:

preview button for notebook

最佳答案

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

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)

不过,这有点 hack,所以我们只是添加了对 knitr::include_graphics 的支持。 .它将在明天的每日(0.99.1272 或更高版本)。

关于r - 使用 knitr::include_graphics() 在 RStudio 笔记本中包含图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38642731/

相关文章:

r - 按日期范围分组

r - 在 RStudio 的 R Notebook 中运行 block 时,如何打印小标题?

r - 在输出中显示 <<..>>=

r - 让knitr运行r脚本: do I use read_chunk or source?

html - 在 Rmarkdown 中用 HTML 文档在左侧添加目录

r - 使用ggplot在多个条形图中添加组信息

r - 用另一个向量子集化一个向量

javascript - 检索 Shiny APP 传单 map 中所有标记的位置

r - 注释掉 Rmd 文件的一些 block /部分

r - kableExtra 包中的 group_rows() 函数不对行进行分组