[英]R Markdown knit fails when path includes “~” for html/pdf document but works for ioslides
從knitr 1.12開始,具有功能include_graphics。 來自?include_graphics:
使用此功能的主要優點是,它可移植,因為它適用於knitr支持的所有文檔格式,因此您無需考慮是否必須使用例如LaTeX或Markdown語法來嵌入外部圖像。
似乎在編織ioslides Rmarkdown演示文稿時include_graphics()
處理路徑中帶有“〜”的路徑(到用戶目錄的快捷方式),但是在編織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")
```
處理文件: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")
```
處理文件: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。
為什么在一種情況下失敗,而在另一種情況下失敗?
如果您使用path.expand('~/path')
代替,那應該可以。 問題是~
的分辨率。 一些函數為您調用path.expand
其他一些函數可能需要您自己執行。 如果事物是相對設置的,則可以選擇相對路徑或相對路徑。 我更喜歡相對路徑方法,因為它在R
和github等中都能很好地工作。
編輯:每@一輝的回應 - normalizePath
是要牢記的另一個工具。
在您的示例中:
...
include_graphics(path.expand("~/Desktop/Rlogo.png"))
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.