如何在 RMarkdown 文档中为参考书目 YAML 元素使用 URL?

How can I use a URL for the bibliography YAML element in an RMarkdown document?

目标: 我可以使用 URL 为我的 YAML 前端内容中的文档指定 csl,我想能够对 bibliography 元素执行相同的操作。

问题:这可能吗?

动机: 对这些元素使用 URLs,而不是直接 links 到我工作目录中的文件,提高了我的可重复性通过合并 publicly-available .csl.bib 文件来呈现文档渲染工作流程,这些文件在更新时也很容易同步到一个中央位置(注意:只要复制者有互联网连接).

次优替代方案:替代方案可能是将 .bib.csl 文件包含在工作目录中,确保它们与项目。然后它们也可以离线使用。这种方法有两个缺点:

  1. 这将导致我的计算机上有许多 .bib.csl 文件的副本(我有文档要呈现的每个项目的副本)
  2. .bib 文件是通过引用管理器自动生成的,因此不能在不破坏引用管理器和 .bib 文件之间的同步的情况下将其放入多个位置(针对不同的项目)。

示范:

这是一个使用 URL 作为 csl 元素的工作示例(直接来自官方引用样式语言 GitHub 存储库的 ecology.csl 文件:

reprex_library.bib

@Misc{Chang2015,
  Title                    = {shiny: Web Application Framework for R. R package version 0.12.1},

  Author                   = {Chang, W. and Cheng, J. and Allaire, JJ. and Xie, Y. and McPherson, J. },
  Year                     = {2015},

  Type                     = {Computer Program},
  Url                      = {http://CRAN.R-project.org/package=shiny}
}


@Article{RCoreTeam,
  Title                    = {R: A Language and Environment for Statistical Computing},
  Author                   = {{R Core Team}},
  Year                     = {2015},

  Type                     = {Journal Article},
  Url                      = {http://www.R-project.org}
}

reprex_paper_bib-direct_csl-link.Rmd

---
title: 'My Title'
author: "Me me me me!"
output: pdf_document
bibliography: reprex_library.bib
csl: https://raw.githubusercontent.com/citation-style-language/styles/master/ecology.csl
---

Application written in the R programming language [@RCoreTeam] using the Shiny framework [@Chang2015].

# REFERENCES

此输出成功:

processing file: reprex_paper_bib-direct_csl-direct.Rmd
  |.................................................................| 100%
  ordinary text without R code


/usr/local/bin/pandoc +RTS -K512m -RTS reprex_paper_bib-direct_csl-direct.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output reprex_paper_bib-direct_csl-direct.tex --template /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' --filter /usr/local/bin/pandoc-citeproc 
output file: reprex_paper_bib-direct_csl-direct.knit.md


Output created: reprex_paper_bib-direct_csl-direct.pdf

Image of successfully rendered pdf with URL to .csl file

这是一个无法使用的示例,它试图用 public 网络 link 填充 bibliography 元素到我的 Dropbox 中相同的 reprex_library.bib 文件:

reprex_paper_bib-link_csl-link.Rmd

---
title: 'My Title'
author: "Me me me me!"
output: html_document
bibliography: https://dl.dropboxusercontent.com/s/3e601ma7ji9iu9z/reprex_library.bib
csl: https://raw.githubusercontent.com/citation-style-language/styles/master/ecology.csl
---

Application written in the R programming language [@RCoreTeam] using the Shiny framework [@Chang2015].

# REFERENCES

此输出中断:

|.................................................................| 100%
  ordinary text without R code


/usr/local/bin/pandoc +RTS -K512m -RTS reprex_paper_bib-link_csl-link.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output reprex_paper_bib-link_csl-link.tex --template /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' --filter /usr/local/bin/pandoc-citeproc 


processing file: reprex_paper_bib-link_csl-link.Rmd
output file: reprex_paper_bib-link_csl-link.knit.md

Could not find bibliography file: https://dl.dropboxusercontent.com/s/3e601ma7ji9iu9z/reprex_library.bib
Error running filter /usr/local/bin/pandoc-citeproc:
Filter returned error status 1
Error: pandoc document conversion failed with error 83
Execution halted

这是我的 R 会话信息:

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.4.2  backports_1.1.2 magrittr_1.5    rsconnect_0.8.5 rprojroot_1.3-2
 [6] htmltools_0.3.6 tools_3.4.2     rticles_0.4.1   yaml_2.1.16     Rcpp_0.12.13   
[11] stringi_1.1.5   rmarkdown_1.8.7 knitr_1.18      stringr_1.2.0   digest_0.6.14  
[16] evaluate_0.10.1

我不知道这是错误还是预期的行为。如果是有意的,我不确定为什么。任何帮助将不胜感激!

编辑:

从一些进一步的阅读来看,这可能是 pandoc-citeproc 的预期行为,它在编织过程中使用 --filter /usr/local/bin/pandoc-citeproc

调用

pandoc-citeproc 手册来看,似乎 csl YAML 元素是有意允许的 "path or URL of a CSL style file",而 bibliography 元素只能是 "a path, or YAML list of paths, of bibliography files to use"。

我已经为这个问题添加了 pandoc 和 pandoc-citeproc 标签,以防它与工作流的那些部分相关。

在代码块中下载文件似乎可行。我将示例文档放在 Untitled.Rmd 中,然后 运行 这有效:

---
title: 'My Title'
author: "Me me me me!"
output: html_document
bibliography: Untitled_files/reprex.bib
csl: https://raw.githubusercontent.com/citation-style-language/styles/master/ecology.csl
---

Application written in the R programming language [@RCoreTeam] using the Shiny framework [@Chang2015].

```{r}
dir.create("Untitled_files")
download.file("https://dl.dropboxusercontent.com/s/3e601ma7ji9iu9z/reprex_library.bib", "Untitled_files/reprex.bib")
```

# REFERENCES

目录名Untitled_files对于Untitled.Rmd来说很神奇:对于foo.Rmdfoo_files是一个目录,在编织后会被清理。可能在某些情况下 knitr 会创建它,然后我的 dir.create 会导致警告,但在这个简单的示例中这是必要的。