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?