编织器在文档中产生错误,但无论如何均能正确生成图形

And*_*rew 5 r knitr r-markdown

我在macOS上编织了一个R Markdown文件,并knitr::opts_chunk$set(dev = c("png", "cairo_pdf"))用来将图的输出同时保存为PNG和PDF文件。我还使用了Cairo PDF库,因为它默认情况下可以正确嵌入字体(请参阅此处

当我编织并创建使用自定义字体的绘图时,knitr会使用开罗正确地保存PNG和PDF文件:

图形输出

但是,在实际的R Markdown针织文档中,R抱怨缺少字体并提供了数十种警告。这很奇怪,因为它在后台运行良好。

这是MWE:

---
title: "So many warnings?"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.path = "fig/",  # Save images to a subdirectory
                      echo = FALSE,  # Hide code for now
                      dpi = 300,  # High resolution PNGs
                      # Save all figures as Cairo PDFs and PNGs
                      dev = c("png", "cairo_pdf"),
                      dev.args = list(png = list(type = "cairo")))
```

```{r load-libraries}
library(ggplot2)
```

```{r warningless-plot}
# This will save two files in the fig/ folder, both saved using Cairo:
# - fig/warningless-plot-1.png
# - fig/warningless-plot-1.pdf

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()
```

```{r warningful-plot}
# This will save two files in the fig/ folder, both saved *correctly* using Cairo:
# - fig/warningful-plot-1.png
# - fig/warningful-plot-1.pdf

# However, rmarkdown or knitr or something in the pipeline gets mad and throws 
# a ton of warnings.

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  theme_grey(base_family = "Comic Sans MS")
```
Run Code Online (Sandbox Code Playgroud)

图形本身已正确保存,但是HTML输出中充满以下警告:

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :    
## font family 'Comic Sans MS' not found in PostScript font database
Run Code Online (Sandbox Code Playgroud)

现在,我的解决方案是将生成带有自定义字体的图形的所有其他块添加warning=FALSE到块选项warningful-plot。我想知道为什么这些额外的警告会发生,以及是否有一种方法可以避免一开始就收到警告。

And*_*rew 5

在这里回答我自己的问题...

根据GitHub上的几个问题(在knitrhrbrthemes处),发生这种情况是因为knitr pdf(NULL)在实际编织时会在后台无形地使用null PDF设备()。但是pdf(),R中的默认图形设备无法处理自定义字体,因此会出现警告。pdf()我猜,即使可见的图形都没有通过基本设备,但它们仍然是看不见的。

使用进行编织时dev = 'png',knitr将使用不可见的png()设备,并且不会引发警告。似乎同时使用cairo_pdf设备会破坏这一点,并迫使knitr返回到不可见的,无需自定义字体的pdf()设备。

我们可以根据以下评论通过强制knitr使用不可见的png()设备来解决此问题

# Use invisible NULL png() device
options(device = function(file, width, height) {
  png(tempfile(), width = width, height = height)
})

# knit options, including `dev = c("png", "cairo_pdf")`
knitr::opts_chunk$set(fig.path = "fig/",  # Save images to a subdirectory
                      echo = FALSE,  # Hide code for now
                      dpi = 300,  # High resolution PNGs
                      # Save all figures as Cairo PDFs and PNGs
                      dev = c("png", "cairo_pdf"),
                      dev.args = list(png = list(type = "cairo")))
Run Code Online (Sandbox Code Playgroud)

那个options(device = ...)咒语使警告消失了。