mikoontz
mikoontz

Reputation: 592

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

Goal: I am able to use a URL to specify the csl for my document within my YAML front matter, and I'd like to be able to do the same for the bibliography element.

Question: Is this possible?

Motivation: Using URLs for these elements, as opposed to direct links to files in my working directory, enhances the reproducibility of my document rendering workflow by incorporating publicly-available .csl and .bib files that are also easily synced to one central location as they are updated (caveat: as long as the reproducer has an internet connection).

A suboptimal alternative: An alternative might be to include the .bib and .csl files in the working directory, ensuring that they stay bundled with the project. Then they're also available offline. There are 2 downsides to this approach:

  1. This will result in many copies of the .bib and .csl files on my computer (a copy for each project in which I have a document to render)
  2. The .bib file is generated automatically via a reference manager and so cannot be put into multiple locations (for different projects) without breaking the synchronization between the reference manager and the .bib file.

Demonstration:

Here's a working example using a URL for the csl element (the ecology.csl file directly from the official Citation Style Language GitHub repository :

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

Which succeeds with this output:

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

Here's a non-working example trying to fill the bibliography element with a public web link to that same reprex_library.bib file housed in my Dropbox:

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

Which breaks with this output:

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

This is my R session info:

> 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

I don't know if this is a bug or intended behavior. If it is intended, I'm not sure why. Any help would be greatly appreciated!

Edit:

From some further reading, perhaps this is intended behavior of pandoc-citeproc which is invoked during the knitting process using --filter /usr/local/bin/pandoc-citeproc

From the pandoc-citeproc manual, it seems like the csl YAML element is intentionally allowed to be a "path or URL of a CSL style file", while the bibliography element can only be "a path, or YAML list of paths, of bibliography files to use".

I've added the pandoc and pandoc-citeproc tags to this question in case it turns out to be related to those parts of the workflow.

Upvotes: 3

Views: 1438

Answers (1)

user2554330
user2554330

Reputation: 44788

It seems to work to download the file in a code chunk. I put the sample document in Untitled.Rmd, then running this works:

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

The directory name Untitled_files is magic for Untitled.Rmd: for foo.Rmd, foo_files is a directory which will be cleaned up after knitting. There are probably circumstances where knitr will create it, and then my dir.create will lead to a warning, but it is necessary in this simple example.

Upvotes: 2

Related Questions