Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this article we will look at how to create a bibliography using the rcrossref package in R with RStudio. To help us get started we will also look at how to reference any R packages that you use in your work with knitr. The article is part of work in progress for the WIPO Handbook on Patent Analytics. Comments and suggestions are welcome.

Many of us will have spent an unreasonable amount of our time struggling to create bibliographies for a thesis, publication or reports. Software such as Endnote (Clarivate Analytics), Mendeley (Elsevier) and Zotero provide options for creating and managing bibliographic data. From experience, this generally involves masses of hours fiddling around with reference formats and so on before and after submission for publication.

Crossref is a non-profit organisation that provides metadata on over 96 million publications in the form of information on the authors, titles, organisations, funding and so on. It is not a full text database but is increasingly providing access to abstracts and links to full texts.

Crossref can be accessed through an API. For R users, Scott Chamberlain, Carl Boettiger, Ted Hart and Karthik Ram have developed the rcrossref package as part of the ever growing suite of ROpenSci packages.

To get started we will install the packages we are going to need and then look at how to quickly create a bibliography file for packages we are going to use. Once we have a grip on that we will use rcrossref.

### Packages

If you are new to R and RStudio then first we need to get set up. To install R for your operating system choose the appropriate option here and install R. Then download the free RStudio desktop for your system here.

You should already have knitr installed with RStudio but in the wildly unlikely event that you don’t then run this line.

install.packages("knitr")
install.packages("devtools")

We will use the development version of rcrossref for reasons we will explain below. You will need devtools (above) installed to do that.

devtools::install_github("ropensci/rcrossref")
library(knitr)
library(rcrossref)

### Creating A Bibliography File

One of the puzzling things about getting started with creating bibliographies in RStudio… is how to create a bibliography file. So, as a warm up let’s do that.

Yihui Xie has made it incredibly easy to create a bibliography in bibtex format with the knitr package. Let’s create a file that contains packages mentioned in this article. The write_bib() function allows us to add in a vector of package names, and to specify the path and name for the file using file =. In this case I am creating a file called packages.bib in the content section of my blogdown site. I’ll break it up for visibility.

knitr::write_bib(c("devtools", "base", "rcrossref", "blogdown", "bookdown", "knitr",
"rmarkdown", "citr", "bibtex"), width = 60,
file = "/Users/pauloldham17inch/blog/content/post/packages.bib")

This takes citation information from the description section of the packages. When you run this chunk it is normal to see a bunch of warning messages such as DESCRIPTION file of package ‘ggmap’could not determine year for ‘ggmap’ from package etc.

It is that easy. All we need to do is identify where we want to save the file and then try to remember the packages that we use regularly.

If we take a quick look at the content we will see the following.

@Manual{R-rcrossref,
title = {rcrossref: Client for Various 'CrossRef' 'APIs'},
author = {Scott Chamberlain and Carl Boettiger and Ted Hart
and Karthik Ram},
note = {R package version 0.8.1.9513},
url = {https://github.com/ropensci/rcrossref},
year = {2018},
}

So now we have an easy way to create a bibliography file, hurrah! Let’s move on to rcrossref.

RStudio includes a button at the top of the screen called Addins. It’s here:

The standard version of rcrossref includes an addin that allows you to search for references using dois (document identifiers). I’ll come back to that. However, the add in for the development version does more thanks to great work by Hao Zhu of giphyr fame. In response to a request on github here, Hao Zhu created a new version of the rcrossref addin that we will use now.

From the Addins menu select Add Rcrossref Citations and a panel will open up. In the development version we can type in the name of an article and then Add to My Citations and Done when we are finished as we see in this example. I’ll look up one I can remember.

We can also use document identifiers. Here is one of mine on the completely bonkers subject of global climate engineering or geoengineering with doi: 10.1098/rsta.2014.0065.

The rcrossref add in creates a new file in your RStudio project directory (assuming you are using projects which is a very good thing), called crossref.bib. Once this file is created any time that you use the Addin it will add references rather than overwrite the file. So, this can be a very good way to build up a bibliography using rcrossref.

To add a citation, using the example above, we simply use [@R-rcrossref], as explained by Yihui here. Note when inserting the reference we skip the type (e.g. Manual, Article etc) at the start of the crossref.bib entry. The result will look something like this:

rcrossref (Chamberlain et al. 2018) is a great package for accessing data for a bibliography or for larger scale bibliometrices. In 2014 Paul Oldham and his co-authors published an article on the completely mad subject of global climate engineering that succeeded in exciting consipiracy theorists using the #contrails and #chemtrails hashtags on twitter (Oldham et al. 2014).

This is ridiculously easy to do.

We now have two bibliographies, one called packages.bib and one called crossref.bib. To get them to render when we knit the document (or build for blogdown or bookdown users… no knitting please) we need to put them in the header or YAML of the rmarkdown document. In all cases that looks like this.

bibliography: [packages.bib, crossref.bib]

When we knit or build the document the citations will show up in the text itself and at the bottom of the document. To mark the start of the bibliography simply create a header such as #References as the final line of your document.

### Going Further

Thanks to the team behind rcrossref you now have over 96 million references at your finger tips. Many thanks are also due to Yihui Xie and the team behind knitr, bookdown and blogdown for their really great work

You can read more about bibliographies on the RStudio site here. Yihui Xie’s knitr website and book are great resources (Xie 2016a).

Bookdown users can find more information in the electronic version of the bookdown book right here (Xie 2016b).

For blogdown users: Alison Presmanes Hill, a co-author of blogdown, has written a very useful guide to citations in the minimal blogdown demo here that I used to write this article. It covers subjects such as using nocite: in your YAML to avoid academic style citations for packages in the text. In the references below you will see that there all the packages used are included but without inline citation. You will very probably want to read the excellent blogdown book here as well (Xie, Thomas, and Hill 2017).

Packages to help you navigate citations include RefManageR, citr and bibtex and `bib2df you may well want to check them out. bib2df may be useful for cleaning up bibliography entries as it provides functions to convert a .bib file to a data.frame and back again.1 This list is not comprehensive and so if you know of any more please feel welcome to mention them below.

# References

Chamberlain, Scott, Carl Boettiger, Ted Hart, and Karthik Ram. 2018. Rcrossref: Client for Various ’Crossref’ ’Apis’. https://github.com/ropensci/rcrossref.

Oldham, P., B. Szerszynski, J. Stilgoe, C. Brown, B. Eacott, and A. Yuille. 2014. “Mapping the Landscape of Climate Engineering.” Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences 372 (2031). The Royal Society: 20140065–5. https://doi.org/10.1098/rsta.2014.0065.

Xie, Yihui. 2016a. Dynamic Documents with R and Knitr. Chapman; Hall/CRC. https://doi.org/10.1201/b15166.

———. 2016b. Bookdown. CRC Press. https://doi.org/10.1201/9781315204963.

Xie, Yihui, Amber Thomas, and Alison Presmanes Hill. 2017. Blogdown. Chapman; Hall/CRC. https://doi.org/10.1201/9781351108195.

1. At the time of writing install using devtools::install_github(“ottlngr/bib2df”)