当HTML / PDF文档的路径包含“〜”但对ioslides有效时,R Markdown编织失败

jza*_*dra 1 r knitr r-markdown

从knitr 1.12开始,具有功能include_graphics。来自?include_graphics:

使用此功能的主要优点是,它可以移植到knitr支持的所有文档格式的意义上,因此是可移植的,因此您无需考虑是否必须使用例如LaTeX或Markdown语法来嵌入外部图像。

似乎include_graphics()在编织ioslides Rmarkdown演示文稿时处理路径中带有“〜”的路径(到用户目录的快捷方式),但是在编织word / html文档时失败。

ioslide的可复制示例(请注意,这些将在您的桌面上复制Rlogo.png:

---
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")
```
Run Code Online (Sandbox Code Playgroud)

处理文件: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-电子邮件混淆无-自包含-变量过渡= 0.4-模板/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rmarkdown/rmd/ioslides/default.html --include-in-标头/var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpKAAz4I/rmarkdown-str91efb7c6c16.html --mathjax --variable'mathjax-url:https ://mathjax.rstudio.com/latest/MathTeXX.js?config = -AMS-MML_HTMLorMML '输出文件:rlogo.knit.md

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

创建的输出:rlogo.html

Word / html文档的可再现失败:

---
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")
```
Run Code Online (Sandbox Code Playgroud)

处理文件: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-电子邮件混淆无-自包含-独立--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-网址:https ://mathjax.rstudio.com/latest/MathJax.js?config = TeX-AMS-MML_HTMLorMML'输出文件:rlogo.knit.md

pandoc:无法获取〜/ Desktop / Rlogo.png〜/ Desktop / Rlogo.png:openBinaryFile:不存在(不存在此类文件或目录)错误:pandoc文档转换失败,错误67暂停执行

我正在使用MacOS Sierra。

为什么在一种情况下失败,而在另一种情况下失败?

col*_*ole 5

path.expand('~/path')我希望,如果您使用它应该可以工作。问题是解决方案~。一些功能需要path.expand您-其他功能可能需要您自己执行。如果事物是​​相对设置的,则可以选择相对路径或相对路径。我更喜欢相对路径方法,因为它在Rgithub 中以及与github等中都能很好地工作。

编辑:根据@yihui的回复 - normalizePath是另一个要记住的工具。

在您的示例中:

...
include_graphics(path.expand("~/Desktop/Rlogo.png"))
...
Run Code Online (Sandbox Code Playgroud)