Chapter 21 Managing References And Citations

21.1 Managing the references for a publication

Create a public Zotero group at https://zotero.org. If you are logged into your Zotero account in your desktop Zotero installation, the associated group library will synchronize to your local reference library. You can then drag-and-drop references into the group library.

You can then also give others access to the group and allow them to edit the associated library (if you hadn’t set it up for public editing). This makes it easy to collaborate on a publication and allows everybody to add references.

21.2 Downloading a Zotero group library for use in R Markdown

To download a Zotero group library, you can use the API, specifically with a call to https://api.zotero.org/groups/XXXX/items?format=bibtex, where XXXX should be replaced with the group number, and the format is specified with the format parameter.

When you specify bibtex as format, the Zotero API automatically produces identifiers that you can use to cite the references. The R Markdown documentation recommends using the csljson format, because PanDoc, the conversion engine powering R Markdown’s PDF rendering, internally uses that. However, then you’ll have to create practical identifiers yourself.

In R, you can automate updating the bibliographic database with the following code:

### Replace XXXX with the group number again
bibCon <- url("https://api.zotero.org/groups/XXXX/items?format=bibtex");
bibliography<-readLines(bibCon, warn=FALSE);
close(bibCon);

### Write the bibliography to a file
writeLines(bibliography, "bibliography.bib");

Note that at this moment, the Zotero API only sends a maximum of 100 entries, so if you have a large library, you’ll have to adjust this script a bit to run the call repeatedly.

21.3 Citing sources in R Markdown

To cite sources, you can include their identifier in the text, prepended with an apetail, and between square brackets, for example [@bibtext_identifier_2020]. You can also omit the authors from the reference, or include more text between the parentheses that are produced - for details, see https://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html.