1

I wanted to try using knitr::include graphics (link) in my R-markdown document. I am using it as suggested by this link (using the full path):

---
title: "Sample Document"
output:
  word_document
---

```{r pressure, echo=FALSE, fig.cap="A caption", out.width = '100%'}
knitr::include_graphics("Z:/../SEM/semflow.png")
```

This is the picture. I tried installing the png package and also tried simply using:

```{r}
knitr::include_graphics("Z:/../SEM/semflow.png")
```

The code is accepted by knitr, which means that it found the file, but it shows up in the Word document like this:

enter image description here

Does anyone have an idea how this could happen?

Related question

A related question (without an answer) can be found here.

Help file: ?knitr::include_graphics

enter image description here

Tom
  • 2,173
  • 1
  • 17
  • 44
  • 1
    Whenever possible, you should use relative paths instead of absolute paths (see the help page `?knitr::include_graphics`). Anyway, without a reproducible example, it's hard to tell what the problem is. – Yihui Xie May 04 '22 at 13:26
  • I once had a similar problem where knitr found the file, but the image wasn't found in the resulting document. I was able to solve it by using normalizePath. You might try that. It looks like this: `knitr::include_graphics(normalizePath("../images/whitelogo-blackletter.png"))` – jtbayly May 05 '22 at 14:15
  • @jtbayly Thanks, just tried it. Made no difference regrettably. – Tom May 05 '22 at 14:20
  • @YihuiXie Where does it say that exactly? It is nowhere in my help page.. – Tom May 05 '22 at 14:34
  • Maybe try to get the file path beforehand, e.g. `list.files("SEM/", pattern = ".png", full.names = TRUE)` and pass it then to the `include_graphics()` function. – Julian May 05 '22 at 14:53
  • @Julian Thank you for your comment. Could you explain why that would maybe make a difference? I did try it, but it did not work. – Tom May 05 '22 at 15:05
  • Similar to @jtbayly I assumed that the problem lies within the path of the file and that would have been my approach to debug. – Julian May 05 '22 at 15:09
  • Apparently it was a template issue. When I removed the template I was using it worked perfectly. What do you guys think, should I remove the question or answer it (so that the next person encountering this problem has another idea what to try)? – Tom May 05 '22 at 15:27
  • 1
    @Tom “Where does it say that exactly?” I think you are using an old version of knitr (BTW, it'll be great if you provide the session info every time, e.g., `xfun::session_info('rmarkdown')`). You may [update packages](https://yihui.org/en/2017/05/when-in-doubt-upgrade/) and retry. – Yihui Xie May 05 '22 at 17:10

0 Answers0