【发布时间】: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 的块选项以及所有其他生成具有自定义字体的图的块中。不过,我想知道为什么会出现这些额外的警告,以及是否有办法避免一开始就收到警告。
【问题讨论】:
-
这似乎是一个更普遍的问题? github.com/yihui/knitr/issues/729
-
还有一个它出现的地方:github.com/hrbrmstr/hrbrthemes/issues/2
标签: r r-markdown knitr