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

  1. toBibtex(citation("xaringan"))
  1. @Manual{,
  2. title = {xaringan: Presentation Ninja},
  3. author = {Yihui Xie},
  4. year = {2020},
  5. note = {R package version 0.18},
  6. url = {https://CRAN.R-project.org/package=xaringan},
  7. }

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

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

  1. knitr::write_bib(c("knitr", "rmarkdown"), width = 60)
  1. @Manual{R-knitr,
  2. title = {knitr: A General-Purpose Package for Dynamic
  3. Report Generation in R},
  4. author = {Yihui Xie},
  5. note = {R package version 1.30.2},
  6. url = {https://yihui.org/knitr/},
  7. year = {2020},
  8. }
  9. @Manual{R-rmarkdown,
  10. title = {rmarkdown: Dynamic Documents for R},
  11. author = {JJ Allaire and Yihui Xie and Jonathan McPherson
  12. and Javier Luraschi and Kevin Ushey and Aron Atkins
  13. and Hadley Wickham and Joe Cheng and Winston Chang and
  14. Richard Iannone},
  15. year = {2020},
  16. note = {R package version 2.5},
  17. url = {https://github.com/rstudio/rmarkdown},
  18. }
  19. @Book{knitr2015,
  20. title = {Dynamic Documents with {R} and knitr},
  21. author = {Yihui Xie},
  22. publisher = {Chapman and Hall/CRC},
  23. address = {Boca Raton, Florida},
  24. year = {2015},
  25. edition = {2nd},
  26. note = {ISBN 978-1498716963},
  27. url = {https://yihui.org/knitr/},
  28. }
  29. @InCollection{knitr2014,
  30. booktitle = {Implementing Reproducible Computational
  31. Research},
  32. editor = {Victoria Stodden and Friedrich Leisch and Roger
  33. D. Peng},
  34. title = {knitr: A Comprehensive Tool for Reproducible
  35. Research in {R}},
  36. author = {Yihui Xie},
  37. publisher = {Chapman and Hall/CRC},
  38. year = {2014},
  39. note = {ISBN 978-1466561595},
  40. url = {http://www.crcpress.com/product/isbn/
  41. 9781466561595},
  42. }
  43. @Book{rmarkdown2018,
  44. title = {R Markdown: The Definitive Guide},
  45. author = {Yihui Xie and J.J. Allaire and Garrett
  46. Grolemund},
  47. publisher = {Chapman and Hall/CRC},
  48. address = {Boca Raton, Florida},
  49. year = {2018},
  50. note = {ISBN 9781138359338},
  51. url = {https://bookdown.org/yihui/rmarkdown},
  52. }
  53. @Book{rmarkdown2020,
  54. title = {R Markdown Cookbook},
  55. author = {Yihui Xie and Christophe Dervieux and Emily
  56. Riederer},
  57. publisher = {Chapman and Hall/CRC},
  58. address = {Boca Raton, Florida},
  59. year = {2020},
  60. note = {ISBN 9780367563837},
  61. url = {https://bookdown.org/yihui/rmarkdown-cookbook},
  62. }

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

  1. ---
  2. bibliography: [packages.bib, references.bib]
  3. ---
  4. ```{r, include=FALSE}
  5. knitr::write_bib(file = 'packages.bib')
  6. ```

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.