Chapter 24 Managing References And Citations
24.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.
24.2 Downloading a Zotero group library for use in R Markdown
ufs R package has a function that downloads all references in a public group and stores them locally so you can use them in your R Markdown files. You call it with two arguments: first, the group number, and second, the filename where you would like to store the references. For example, to save all reference in group
2425237 in file
bibliography.bib in the root of your project directory using the brilliant
here package to locate that directory, you can use:
::zotero_download_and_export_items( ufs2425237, ::here("bibliography.bib") here);
The following gives a rudimentary insight in what this funciton does behind the scenes. The basics use the Zotero API, specifically a call to
XXXX should be replaced with the group number, and the format is specified with the
When you specify
bibtex as format, the Zotero API automatically produces identifiers that you can use to cite the references. Note that 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.
To call this API from R and automate update the bibliographic database, you can use with the following code:
### Replace XXXX with the group number again <- url("https://api.zotero.org/groups/XXXX/items?format=bibtex"); bibCon <-readLines(bibCon, warn=FALSE); bibliographyclose(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. The
ufs function above does that for you.
24.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.