设置 knitr 以输出单独的图像

Setting knitr to output separate images

我正在尝试从 knitr 输出中获取 PNG 图以作为单独的文件写入磁盘,而无需手动执行。

我试过的

都没用。编织过程运行所在的文件夹没有多余的文件,HTML文档在其源代码中有base64嵌入图像。

环境

MWE:RStudio RMarkdown 文件,添加了上述选项:

---
title: "Untitled"
output: html_document
self_contained: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(dev="png", 
                      dev.args=list(type="cairo"),
                      dpi=96)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

self_containedhtml_document 的选项,不是顶级 YAML 设置。下面的文档仅使用它。 PNG 是默认图形类型,因此您无需指定。

---
title: "Untitled"
output: 
  html_document:
    self_contained: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```