Call utils::citation() and utils::toBibtex() to create bib entries for R
packages and write them in a file. It can facilitate the auto-generation of
bibliography databases for R packages, and it is easy to regenerate all the
citations after updating R packages.
Package names. Packages which are not installed are ignored.
The (.bib) file to write. By default, or if NULL, output
is written to the R console.
Whether to fix some known problems in the citations, especially non-standard format of author names.
Width of lines in bibliography entries. If NULL, lines will
not be wrapped.
Prefix string for keys in BibTeX entries; by default, it is
R- unless option('xfun.bib.prefix') has been set to another string.
A vector of path names of R libraries.
Use the URL field from the DESCRIPTION file. See
Details below.
The way to include a date of the form (retrieved YYYY-mm-dd) in
the note field of bib entries. For date = 'none', no date is included.
For date = 'package', the packageDate() of the package is included. For
date = 'today', Sys.Date() is included. Alternatively, you can provide
a date directly, e.g., date = '2025-04-01'.
A list containing the citations. Citations are also written to the
file as a side effect.
For a package, the keyword R-pkgname is used for its bib item, where
pkgname is the name of the package. Citation entries specified in the
CITATION file of the package are also included. The main purpose of
this function is to automate the generation of the package citation
information because it often changes (e.g., the author, year, package
version, and so on).
There are at least two different uses for the URL in a reference list. You
might want to tell users where to go for more information. In that case, use
the default packageURL = TRUE, and the first URL listed in the
DESCRIPTION file will be used. Be careful: some authors don't put the
most relevant URL first. Alternatively, you might want to identify exactly
which version of the package was used in the document. If it was installed
from CRAN or some other repositories, the version number identifies it, and
packageURL = FALSE will use the repository URL (as used by
utils::citation()).
Some packages on CRAN do not have standard bib entries, which was once
reported by Michael Friendly at
https://stat.ethz.ch/pipermail/r-devel/2010-November/058977.html. I
find this a pain, and there are no easy solutions except contacting package
authors to modify their DESCRIPTION files. The argument tweak has
provided hacks to deal with known packages with non-standard bib entries;
tweak = TRUE is by no means intended to hide or modify the original
citation information. It is just due to the loose requirements on package
authors for the DESCRIPTION file. On one hand, I apologize if it really
mangles the information about certain packages; on the other, I strongly
recommend package authors to consider the Authors@R field (see the
manual Writing R Extensions) to make it easier for other people to cite R
packages.
pkg_bib(c("base", "MASS", "xfun"))
#> Warning: could not determine year for ‘xfun’ from package DESCRIPTION file
#> @Manual{R-MASS,
#> title = {MASS: Support Functions and Datasets for Venables and Ripley's MASS},
#> author = {Brian Ripley and Bill Venables},
#> year = {2025},
#> note = {R package version 7.3-65},
#> url = {http://www.stats.ox.ac.uk/pub/MASS4/},
#> }
#>
#> @Manual{R-base,
#> title = {R: A Language and Environment for Statistical Computing},
#> author = {{R Core Team}},
#> organization = {R Foundation for Statistical Computing},
#> address = {Vienna, Austria},
#> year = {2025},
#> url = {https://www.R-project.org/},
#> }
#>
#> @Manual{R-xfun,
#> title = {xfun: Supporting Functions for Packages Maintained by Yihui Xie},
#> author = {Yihui Xie},
#> note = {R package version 0.54},
#> url = {https://github.com/yihui/xfun},
#> year = {2025},
#> }
#>
#> @Book{MASS2002,
#> title = {Modern Applied Statistics with S},
#> author = {W. N. Venables and B. D. Ripley},
#> publisher = {Springer},
#> edition = {Fourth},
#> address = {New York},
#> year = {2002},
#> note = {ISBN 0-387-95457-0},
#> url = {https://www.stats.ox.ac.uk/pub/MASS4/},
#> }
#>
pkg_bib("cluster", prefix = "R-pkg-") # a different prefix
#> @Manual{R-pkg-cluster,
#> title = {cluster: "Finding Groups in Data": Cluster Analysis Extended Rousseeuw et
#> al.},
#> author = {Martin Maechler and Peter Rousseeuw and Anja Struyf and Mia Hubert},
#> year = {2025},
#> note = {R package version 2.1.8.1},
#> url = {https://svn.r-project.org/R-packages/trunk/cluster/},
#> }
#>
pkg_bib("xfun", date = "package")
#> Warning: could not determine year for ‘xfun’ from package DESCRIPTION file
#> @Manual{R-xfun,
#> title = {xfun: Supporting Functions for Packages Maintained by Yihui Xie},
#> author = {Yihui Xie},
#> note = {R package version 0.54 (retrieved 2025-11-18)},
#> url = {https://github.com/yihui/xfun},
#> year = {2025},
#> }
#>