3

As of knitr 1.12, there is the function include_graphics. From ?include_graphics:

The major advantage of using this function is that it is portable in the sense that it works for all document formats that knitr supports, so you do not need to think if you have to use, for example, LaTeX or Markdown syntax, to embed an external image.

It seems that include_graphics() handles paths with "~" in them (shortcut to user directory) when knitting an ioslides Rmarkdown presentation, but fails when knitting a word/html document.

Reproducible example for ioslides (note that these will make a copy of the Rlogo.png onto your desktop:

---
title: "Rlogo ioslides"
output: ioslides_presentation
---

## Images?

```{r}
require(knitr)
rlogo <- paste0(.libPaths(), "/png/img/Rlogo.png")
include_graphics(rlogo)

file.copy(from = rlogo, to = "~/Desktop")
include_graphics("~/Desktop/Rlogo.png")
```

processing file: rlogo.Rmd

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS rlogo.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output rlogo.html --smart --email-obfuscation none --self-contained --variable transition=0.4 --template /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rmarkdown/rmd/ioslides/default.html --include-in-header /var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpKAAz4I/rmarkdown-str91efb7c6c16.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' output file: rlogo.knit.md

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS rlogo.utf8.md --to ioslides_presentation.lua --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output /var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpKAAz4I/ioslides-output91ef159c1e3e.html --slide-level 2

Output created: rlogo.html

And reproducible failure for word/html document:

---
title: "Rlogo word/html"
output:
  html_document: default
  word_document: default
---

## Images?

```{r}
require(knitr)
rlogo <- paste0(.libPaths(), "/png/img/Rlogo.png")
include_graphics(rlogo)

file.copy(from = rlogo, to = "~/Desktop")
include_graphics("~/Desktop/Rlogo.png")
```

processing file: rlogo.Rmd

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS rlogo.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output rlogo.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable 'theme:bootstrap' --include-in-header /var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpCR2Vrn/rmarkdown-str91681de2b0f3.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' output file: rlogo.knit.md

pandoc: Could not fetch ~/Desktop/Rlogo.png ~/Desktop/Rlogo.png: openBinaryFile: does not exist (No such file or directory) Error: pandoc document conversion failed with error 67 Execution halted

I'm using MacOS Sierra.

Why is this failing in one case but not the other?

3
  • What OS are you using? And what is the specific error you are getting? Commented Jun 23, 2017 at 19:20
  • @MrFlick I've added to my original post. Commented Jun 23, 2017 at 20:26
  • FYI, the same question was asked here: github.com/yihui/knitr/issues/1411 Commented Jun 24, 2017 at 2:29

1 Answer 1

7

Should work if you use path.expand('~/path') instead, I would expect. The problem is the resolution of the ~. Some functions call path.expand for you - others might require you to do it yourself. That or relative paths are an option if things are set up relatively. I prefer the relative path approach because it works nicely in R and with github, etc.

EDIT: Per @yihui's response - normalizePath is another tool to keep in mind.

In your example:

...
include_graphics(path.expand("~/Desktop/Rlogo.png"))
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.