设置knitr输出单独的图像

时间:2016-12-01 21:34:02

标签: r png knitr

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

我尝试了什么

都没有奏效。编织进程运行的文件夹没有额外的文件,HTML文档的源代码中有base64嵌入的图像。

环境

  • RStudio 0.99.903
  • R 3.2.3
  • knitr 1.15.1

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.

1 个答案:

答案 0 :(得分:3)

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)
```