Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 771
kneijenhuijs Avatar asked Nov 07 '17 10:11

kneijenhuijs


People also ask

What is knitr in RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.

How do I show images in RMarkdown?

To add an image in markdown you must stop text editing, and you do this with the command [Alt text] precedeed by a ! Then you have to add the path to the image in brackets. The path to the image is the path from your directory to the image.

How do I add graphics to RMarkdown?

Including images in an R markdown File. We can include an image using {knitr} and the include_graphics() function, e.g. Typically the chunk would use echo = FALSE as we don't want to see the actual R code.


1 Answers

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.

like image 124
Michael Harper Avatar answered Oct 12 '22 22:10

Michael Harper