knitr::include_graphics in bookdown, is not rendering the image
Asked Answered
G

1

5

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
Gawain answered 7/11, 2017 at 10:5 Comment(10)
Do you set any global options at the start of the document? My guess would be you might be putting the file path in incorrectly. Include getwd() in a chunk at the beginning to confirm where it is set.Phelps
I don't have any global options. Running getwd() returns the folder in which the index.Rmd file exists, which is the parent folder of the Figures folder I use in my code.Gawain
I should mention that I have tried the absolute path as well, which leads to the same result. The graphic loads in the workbook itself, but is not rendered.Gawain
This is quite hard to work out without seeing your full code. Can you provide the full index file as a Minimal Working Example?Phelps
Why not placing your image with ![figur description blah \label{fig1bla}](path/yourFigure.png) and reference in text with \ref{fig1bla}?Collateral
@jaySf, this won't overcome the primary issue. Plus it goes against the guidance provided in the bookdown guidance to use include_graphicsPhelps
I am not sure, but I would not use a space in a file name. Could you rename the file name of 'My Figure.png' as 'My_Figure.png' ?Draftee
I can hardly believe it, but removing the spaces in the file name, and replacing them with underscores worked! While I always knew this was best practice, in R I had never run into an issue as long as the file name was used in a function where it was in quotes. Guess that's the last time I'll use spaces. Thanks! Edit: If you can add your solution as an answer, I can close this question.Gawain
@dapeng, I have expanded upon your answer and added it below so that it might help others in the future.Phelps
I am glad that it works. It is hard to give a better answer than @Mikey Harper"s. Please accept it if you'd like.Draftee
P
12

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.

Phelps answered 10/11, 2017 at 17:47 Comment(3)
Good explanation. Thank you for clear it. Could you kindly change the citation of my nickname 'dapeng' as my real name 'pzhao'? I changed it in my profile and found the citation was not updated automatically. But it cannot be changed back in 30 days.Draftee
\includegraphics{} you dropped the s in graphics.Kerplunk
Cheers @dxander, feel free to edit the answers on SO if you see any typos, it is always useful :)Phelps

© 2022 - 2024 — McMap. All rights reserved.