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

时间:2019-03-08 23:13:09

标签: r r-markdown knitr

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

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

figure output

但是,在实际的编织的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的块选项中,以及所有其他使用自定义字体生成图的块。我想知道为什么这些额外的警告会发生,以及是否有一种方法可以避免一开始就收到警告。

1 个答案:

答案 0 :(得分:4)

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

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

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

我们可以通过强制knitr使用不可见的png()设备based on this comment here来解决此问题:

# 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 = ...)的咒语使警告消失了。