knitr 在文档中生成错误,但无论如何都能正确生成图形
knitr generating errors in document but generates figures correctly regardless
我正在 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
的块选项和所有其他生成自定义字体图的块。不过,我想知道为什么会出现这些额外的警告,以及是否有办法首先避免收到警告。
在这里回答我自己的问题……
根据 GitHub 上的几个问题(在 knitr and hrbrthemes),发生这种情况是因为 knitr 在实际编织时在后台无形地使用了一个空 PDF 设备 (pdf(NULL)
)。不过,R 中的默认 pdf()
图形设备无法处理自定义字体,因此会出现警告。尽管 none 的可见图形曾经穿过基础 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 = ...)
咒语使警告消失。
我正在 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
的块选项和所有其他生成自定义字体图的块。不过,我想知道为什么会出现这些额外的警告,以及是否有办法首先避免收到警告。
在这里回答我自己的问题……
根据 GitHub 上的几个问题(在 knitr and hrbrthemes),发生这种情况是因为 knitr 在实际编织时在后台无形地使用了一个空 PDF 设备 (pdf(NULL)
)。不过,R 中的默认 pdf()
图形设备无法处理自定义字体,因此会出现警告。尽管 none 的可见图形曾经穿过基础 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 = ...)
咒语使警告消失。