Cannot embed images from sub-directory using child rmd

I have a bookdown project that used to build just fine, but after updating everything (R Studio, R, bookdown, etc) this afternoon, it stopped working. I also tried updating to the dev package of bookdown, but it still doesn't work.

The problem is that an image cannot be found, no matter what I try with the path. It is embedded via knitr by a child Rmd file. If I put this in my index.Rmd, it works no problem:

    ```{r, echo=FALSE, fig.align='center', out.width='50%'}

But if I try to include that same image in a child rmd file, it stops working. Here's the replacement line in index.Rmd:

    ```{r, child='basic-test/rmds/classics-cover-page.Rmd'}

And here's the entire contents of classics-cover-page.Rmd:

    ```{r, echo=FALSE, fig.align='center', out.width='50%'}

Building the website doesn't throw an error, but the image is not found when you view the site. Building the epub throws the following error:

[WARNING] Could not fetch resource '../images/am_I_crazy.png': PandocResourceNotFound "../images/am_I_crazy.png"

Building the PDF throws the following error:

! LaTeX Error: File `../images/am_I_crazy' not found.

I'm pretty sure that's the right path, because it used to work, and because otherwise I get an error right away from knitr that it can't find the image. I've tried a bunch of other path combos anyway, but nothing is working. If I've just missed the right path, please let me know how I should reference that image file.

Otherwise, I suspect it's a bug introduced in knitr, since it is finding the image, but then not passing the correct path on to the next stage in the process. Does that seem like a reasonable conclusion? Should I file a bug with knitr?

P.S. I have an example project with this problem on Github that is simply the bookdown demo book with the additions mentioned above.

1 Like

I think this is something due to using relative path in knitr::include_graphics. If you use normalizePath in your child document, it is working.

What happens here it that your block in child document with knitr::include_graphics will generate

<img src="../images/am_I_crazy.png" width="50%" style="display: block; margin: auto;" />

So the path here becomes relative to the HTML file and not the Rmd file as in the first place.

As you are saying this worked before, I am looking for the change in knitr or rmarkdown that create this.

Currently, as I am writing this, I agree with you that this does not return what we expect and maybe knitr can do more on this to transform the path.

There is an option closely related but not exactly the same:

Do you know which version you used before updating ?

About this updating thing, if you don't want your project to break due to updating package, you can have a look at renv :package:.

1 Like

No. Sorry. I did run xfun before updating, but after updating Rstudio, that stuff isn't in my terminal history anymore. If there's a way I can still look it up, I'm happy to do so.

I'll take a look. Thanks.

Brilliant. Thanks. I'm not surprised that there is a way to make it work. Glad I didn't file a bug report.

I see now as I look through that issue and related issues and search about knitr and normalizePath that there has been a fair bit of confusion about this sort of path problem. It does seem like since knitr finds the image, and since it is what generates the files that are then sent to Pandoc, that knitr should get the path right. Perhaps that can be made to work. In the meantime I'll use normalize path. Thanks so much for helping with this.

Yes I think also that there is something that knitr should do. At the end, it is just that curently the path in include_graphics is written unchanged to the markdown file for pandoc to process and at the end, the relative path is not correct for the html output.

I am just curious that it worked before and work no more.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.