knitr::include_graphics 在 bookdown 中,未渲染图像

knitr::include_graphics in bookdown, is not rendering the image

我正在尝试在使用 bookdown 呈现的文件中包含一个 .png 文件。 knitr::include_graphics() 应该是要走的路。

代码:

```{r fig1, fig.cap='My Caption', echo=FALSE, message=FALSE, warning=FALSE}
knitr::include_graphics("./Figures/My Figure.png")
```

在 .Rmd 文件中,我可以 运行 我的 r 块,它呈现下图。所以,路径应该是正确的。但是我在编织章节,或者渲染全书的时候,图没有渲染出来。

会不会是我的一些其他选项覆盖了这个数字?下面是我的 index.Rmd 文件的 YAML header,以及我的 _output.yml 文件中的代码。

--- 
title: "My Title"
author: "My Name"
date: "`r Sys.Date()`"
output: 
  bookdown::gitbook:
    split_by: section
  bookdown::pdf_book:
    keep_tex: no
documentclass: book
classoption: openany
bibliography: [Mybib.bib]
csl: Mycsl.csl
biblio-style: apalike
link-citations: yes
description: "My Description"
---

bookdown::gitbook:
  config:
    toc:
      before: |
        <li><a href="./">Short Title</a></li>
bookdown::pdf_book:
  latex_engine: xelatex
  citation_package: natbib
bookdown::epub_book: default

图片文件路径中有空格。

文件名中的空格在 R 中不是问题,这就是您能够在 RStudio 中看到图像的原因。然而,knitr 有点复杂,因为它实际上执行许多不同的程序(R、pandoc 和 LaTeX)。

knitr, the knitr:: includegraphics function actually executes different results depending on whether the output is HTML, PDF or md. When building a PDF, it is passing the images through LaTeX and uses the function \includegraphics{} to insert the picture. As explained here 指南中所述:

"The file name of the image should not contain white spaces nor multiple dots"

删除空格将解决问题,通常最好在 R 中避免使用空格,即使它们在技术上是允许的。

感谢 @pzhao 最初突出了问题。