本文介绍了当路径包含“〜"时,R Markdown编织失败.适用于html/pdf文档,但适用于ioslides的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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")
```

/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-智能-电子邮件混淆无-自包含-变量转换= 0.4-模板/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 ' 输出文件:rlogo.knit.md

/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-至ioslides_presentation.lua-来自markdown + autolink_bare_uris + ascii_identifiers + tex_math_single_backslash --output/var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpKAAz4I/ioslides-output91ef159c1e3e.html --slide-level 2

/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

创建的输出:rlogo.html

Output created: rlogo.html

Word/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")
```

/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 ' 输出文件:rlogo.knit.md

/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:无法获取〜/Desktop/Rlogo.png〜/Desktop/Rlogo.png: openBinaryFile:不存在(没有这样的文件或目录)错误: pandoc文档转换失败,错误67,执行暂停

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

我正在使用MacOS Sierra.

I'm using MacOS Sierra.

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

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

推荐答案

如果您使用path.expand('~/path'),应该可以.问题是~的分辨率.一些函数为您调用path.expand-其他函数可能需要您自己执行.如果事物是​​相对设置的,则可以选择相对路径或相对路径.我更喜欢相对路径方法,因为它在R和github等上都能很好地工作.

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.

根据@yihui的响应-normalizePath是另一个可以保持的工具记住.

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

在您的示例中:

...
include_graphics(path.expand("~/Desktop/Rlogo.png"))
...

这篇关于当路径包含“〜"时,R Markdown编织失败.适用于html/pdf文档,但适用于ioslides的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:42