4.6 Generate R package citations

To cite an R package, you can use the function citation() from base R. If you want to generate a citation entry for BibTeX, you can pass the returned object of citation() to toBibtex(), e.g.,

toBibtex(citation("xaringan"))
Warning in citation("xaringan"): no date field in
DESCRIPTION file of package 'xaringan'
Warning in citation("xaringan"): could not determine
year for 'xaringan' from package DESCRIPTION file
@Manual{,
  title = {xaringan: Presentation Ninja},
  author = {Yihui Xie},
  note = {R package version 0.16.1},
  url = {https://github.com/yihui/xaringan},
}

To use citation entries generated from toBibtex(), you have to copy the output to a .bib file, and add citation keys (e.g., change @Manual{, to @Manual{R-xaringan,). This can be automated via the function knitr::write_bib(), which generates citation entries to a file and adds keys automatically, e.g.,

knitr::write_bib(c(.packages(), "bookdown"), "packages.bib")

The first argument should be a character vector of package names, and the second argument is the path to the .bib file. In the above example, .packages() returns the names of all packages loaded in the current R session. This makes sure all packages being used will have their citation entries written to the .bib file. When any of these packages are updated (e.g., the author, title, year, or version of a package is changed), write_bib() can automatically update the .bib file.

There are two possible types of citation entries. One type is generated from the package’s DESCRIPTION file, and the other type is generated from the package’s CITATION file if provided. For the former type, the citation keys are of the form R-pkgname, where pkgname is the package name (e.g., R-knitr). For the latter type, the keys are created by concatenating the package name and the publication year (e.g., knitr2015). If there are multiple entries in the same year, a letter suffix will be added, e.g., knitr2015a and knitr2015b. The former type is often used to cite the package itself (i.e., the software), and the latter type often consists of publications related to the package, such as journal papers or books.

knitr::write_bib(c("knitr", "rmarkdown"), width = 60)
@Manual{R-knitr,
  title = {knitr: A General-Purpose Package for Dynamic
    Report Generation in R},
  author = {Yihui Xie},
  note = {R package version 1.29.5},
  url = {https://yihui.org/knitr/},
  year = {2020},
}

@Manual{R-rmarkdown,
  title = {rmarkdown: Dynamic Documents for R},
  author = {JJ Allaire and Yihui Xie and Jonathan McPherson
    and Javier Luraschi and Kevin Ushey and Aron Atkins
    and Hadley Wickham and Joe Cheng and Winston Chang and
    Richard Iannone},
  note = {R package version 2.3.7},
  url = {https://github.com/rstudio/rmarkdown},
  year = {2020},
}

@Book{knitr2015,
  title = {Dynamic Documents with {R} and knitr},
  author = {Yihui Xie},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2015},
  edition = {2nd},
  note = {ISBN 978-1498716963},
  url = {https://yihui.org/knitr/},
}

@InCollection{knitr2014,
  booktitle = {Implementing Reproducible Computational
    Research},
  editor = {Victoria Stodden and Friedrich Leisch and Roger
    D. Peng},
  title = {knitr: A Comprehensive Tool for Reproducible
    Research in {R}},
  author = {Yihui Xie},
  publisher = {Chapman and Hall/CRC},
  year = {2014},
  note = {ISBN 978-1466561595},
  url = {http://www.crcpress.com/product/isbn/
    9781466561595},
}

@Book{rmarkdown2018,
  title = {R Markdown: The Definitive Guide},
  author = {Yihui Xie and J.J. Allaire and Garrett
    Grolemund},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2018},
  note = {ISBN 9781138359338},
  url = {https://bookdown.org/yihui/rmarkdown},
}

@Book{rmarkdown2020,
  title = {R Markdown Cookbook},
  author = {Yihui Xie and Christophe Dervieux and Emily
    Riederer},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2020},
  note = {ISBN 9780367563837},
  url = {https://bookdown.org/yihui/rmarkdown-cookbook},
}

Without the file path argument, knitr::write_bib() writes the citation entries to the R console, as you can see from the above example.

Note that write_bib() is designed to overwrite the existing bibliography file. If you want to manually add any other entries to the bibliography, it is recommended that you create a second .bib file and refer to it in the YAML field bibliography, e.g.,

---
bibliography: [packages.bib, references.bib]
---

```{r, include=FALSE}
knitr::write_bib(file = 'packages.bib')
```

In the above example, packages.bib is automatically generated, and you should not manually change it. All other citation entries can be manually written to references.bib.

We only introduced one way to generate R package citations above. To dynamically generate citations for other types of literature, you may check out the knitcitations package (Boettiger 2019).

References

Boettiger, Carl. 2019. Knitcitations: Citations for Knitr Markdown Files. https://github.com/cboettig/knitcitations.