r - knitr 在文档中生成错误,但无论如何都能正确生成数字

标签 r r-markdown knitr

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

当我编织并创建使用自定义字体的绘图时,knitr 使用 Cairo 正确保存 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 的 block 选项以及使用自定义字体生成绘图的所有其他 block 。不过,我想知道为什么会出现这些额外的警告,以及是否有办法避免首先收到警告。

最佳答案

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

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

当使用 dev = 'png' 编织时,knitr 将使用不可见的 png() 设备,并且不会抛出任何警告。看来,同时使用 cairo_pdf 设备会打破这一点,并迫使 knit 返回到不可见的、无自定义字体的 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 = ...) 咒语使警告消失。

关于r - knitr 在文档中生成错误,但无论如何都能正确生成数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55072289/

相关文章:

r - 将频率和 SD 添加到 R 中的摘要中

r - 如何将变量名称绑定(bind)到 df

r - 如何在 R 中创建零长度的数值向量

r - 使用 RPostgreSQL 列出数据库

r - 可以从 knitr 生成 MediaWiki 标记吗?

r - 是否有自动显示 `stargazer` 表中回归 F 统计量的 p 值的方法?

Windows 上的 RStudio 不使用 XeLaTeX

r - knitr可以根据每个chunk中的R代码结果动态输出叙述文本吗?

r - "' knitr ' not found"打包/小插图构建期间的错误

r - knitr 生产的 latex 图标题中的斜体文本