gpt4 book ai didi

当路径包含 html/pdf 文档的 "~"但适用于 ioslides 时,R Markdown 编织失败

转载 作者:行者123 更新时间:2023-12-04 00:35:06 25 4
gpt4 key购买 nike

从 knitr 1.12 开始,有函数 include_graphics。从 ?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.

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

ioslides 的可重现示例(请注意,它们会将 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")
```

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

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

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

我正在使用 MacOS Sierra。

为什么这在一种情况下失败而在另一种情况下却没有?

最佳答案

如果您改用 path.expand('~/path') 应该可以工作,我希望如此。问题是 ~ 的解析。某些函数会为您调用 path.expand - 其他函数可能需要您自己执行。如果事物是​​相对设置的,则该路径或相对路径是一个选项。我更喜欢相对路径方法,因为它在 R 和 github 等中工作得很好。

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

在你的例子中:

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

关于当路径包含 html/pdf 文档的 "~"但适用于 ioslides 时,R Markdown 编织失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44728551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com