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
kneijenhuijs
  • 1,189
  • 1
  • 12
  • 21
  • 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. – Michael Harper Nov 07 '17 at 10:49
  • 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. – kneijenhuijs Nov 07 '17 at 11:42
  • 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. – kneijenhuijs Nov 07 '17 at 11:43
  • 2
    This is quite hard to work out without seeing your full code. Can you provide the full index file as a Minimal Working Example? – Michael Harper Nov 07 '17 at 11:57
  • Why not placing your image with `![figur description blah \label{fig1bla}](path/yourFigure.png)` and reference in text with `\ref{fig1bla}`? – jay.sf Nov 07 '17 at 18:08
  • 1
    @jaySf, this won't overcome the primary issue. Plus it goes against the guidance provided in the bookdown guidance to use `include_graphics` – Michael Harper Nov 07 '17 at 23:19
  • 4
    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' ? – pzhao Nov 08 '17 at 08:22
  • 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. – kneijenhuijs Nov 08 '17 at 08:45
  • @dapeng, I have expanded upon your answer and added it below so that it might help others in the future. – Michael Harper Nov 10 '17 at 17:51
  • 1
    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. – pzhao Nov 11 '17 at 07:34

1 Answers1

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.

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • 2
    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. – pzhao Nov 11 '17 at 10:49
  • \includegraphics{} you dropped the s in graphics. – dxander Aug 08 '19 at 19:10
  • Cheers @dxander, feel free to edit the answers on SO if you see any typos, it is always useful :) – Michael Harper Aug 08 '19 at 19:59