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

百般思念 提交于 2019-12-08 07:34:06

问题


I am trying to include a .png file in a file that I am rendering using bookdown. knitr::include_graphics() should be the way to go.

The code:

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

In the .Rmd file, I can run my r block, and it renders the image below. So, the path should be correct. However, when I knit the chapter, or render the entire book, the figure is not rendered.

Could it be that some of my other options are overriding the figure? Below are my YAML header of the index.Rmd file, and the code in my _output.yml file.

--- 
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

回答1:


You have spaces in the image file path.

Spaces in file names are not a problem within R, and that is why you were able to see the image within RStudio. However, knitr is a bit more complex as it actually executes a number of different programs (R, pandoc & LaTeX).

As explained within the guidance to 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"

Removing spaces will fix the problem, and it would generally be good practise to avoid them in R even if they technically are allowed.

Credit to @pzhao for initially highlighting the problem.



来源:https://stackoverflow.com/questions/47155042/knitrinclude-graphics-in-bookdown-is-not-rendering-the-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!