【问题标题】:knitr generating errors in document but generates figures correctly regardlessknitr 在文档中生成错误,但无论如何都会正确生成数字
【发布时间】:2019-07-31 00:31:22
【问题描述】:

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

当我编织并创建使用自定义字体的绘图时,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 的块选项以及所有其他生成具有自定义字体的图的块中。不过,我想知道为什么会出现这些额外的警告,以及是否有办法避免一开始就收到警告。

【问题讨论】:

标签: r r-markdown knitr


【解决方案1】:

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

根据 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 = ...) 咒语使警告消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2022-07-23
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2016-10-30
    • 2018-03-26
    相关资源
    最近更新 更多