首页 > 解决方案 > knitr 在文档中生成错误,但无论如何都会正确生成数字

问题描述

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

当我编织并创建使用自定义字体的绘图时,knitr 使用 Cairo 正确保存了 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")
```

数字本身已正确保存,但 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

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

标签: rr-markdownknitr

解决方案


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

根据 GitHub 上的几个问题(在knitrhrbrthemes 上pdf(NULL)),发生这种情况是因为 knitr在实际编织时在后台不可见地使用了一个空 PDF 设备( )。但是,R 中的默认pdf()图形设备无法处理自定义字体,因此会出现警告。尽管没有任何可见图形通过基本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")))

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


推荐阅读