【问题标题】:Include images in RStudio notebook with knitr::include_graphics()使用 knitr::include_graphics() 在 RStudio 笔记本中包含图像
【发布时间】:2016-12-03 05:18:56
【问题描述】:

我对即将推出的 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')
```

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

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

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

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

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

【问题讨论】:

  • 您想使用r codermarkdown 语法来获取图像吗?请确认
  • 我想使用r-code,最好是knitr::include_graphics()
  • 抱歉,我误读了您的陈述。我想将图像包含在 r-markdown 代码块中
  • 哦,让我看看。因此,您想在 r 块中读取图像,并且当您编织 r 笔记本时通过 r markdown 时,图像应该在那里?对吗?
  • 我想是的。澄清一下:我不编织笔记本,而是预览它。

标签: r rstudio knitr r-markdown


【解决方案1】:

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

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 或更晚)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2017-04-17
    相关资源
    最近更新 更多