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

时间:2018-01-26 13:10:21

标签: r knitr r-markdown pandoc pandoc-citeproc

目标:我可以使用网址在我的YAML前端内为我的文档指定csl,我希望能够为此做出相同的操作bibliography元素。

问题:这可能吗?

动机:使用这些元素的网址,而不是指向我工作目录中文件的直接链接,通过合并公开提供的.csl和{来增强文档呈现工作流程的可重复性{1}}文件在更新时也很容易同步到一个中心位置(警告:只要重现器具有互联网连接)。

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

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

    以下是使用.bib元素的网址(ecology.csl文件直接来自官方Citation Style Language GitHub存储库的工作示例:

    reprex_library.bib

    csl

    reprex_paper_bib-direct_csl-link.Rmd

    @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}
    }
    

    使用此输出成功:

    ---
    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
    

    Image of successfully rendered pdf with URL to .csl file

    这是一个非工作示例,尝试使用公共网络链接填充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 元素,该链接指向我Dropbox中的同一个reprex_library.bib文件:

    reprex_paper_bib-link_csl-link.Rmd

    bibliography

    这打破了这个输出:

    ---
    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

    在编织过程中调用

    pandoc-citeproc手册中,似乎有意允许--filter /usr/local/bin/pandoc-citeproc YAML元素成为“CSL样式文件的路径或URL”,而csl元素只能是“要使用的参考书目文件的路径或YAML路径列表”。

    我已将pandoc和pandoc-citeproc标记添加到此问题中,以防它与工作流的这些部分相关。

1 个答案:

答案 0 :(得分:1)

似乎可以在代码块中下载文件。我将示例文档放在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会导致警告,但在这个简单的示例中这是必要的。