How do I reference R packages in an R markdown bibiliography?

I'm trying to write a scientific manuscript entirely in an R project. R markdown and bookdown have gotten me 99% of the way there, but I still can't figure out how to properly reference R packages in the bibliography. The author, title, and year show up, but I can't get it to add the version information and URL.

I put a minimum working example in a GitHub repository. You can download that and knit the .Rmd file, or follow these steps.

  1. Create a file called references.bib with an entry for an R package. This example uses a lightly edited version of the BibTeX entry generated using citation() for package pracma.
@Manual{R-pracma,
  title = {pracma: Practical Numerical Math Functions},
  author = {Hans W. Borchers},
  year = {2021},
  note = {R package version 2.3.3},
  url = {https://CRAN.R-project.org/package=pracma},
}
  1. Download a citation style language (CSL) template. I used the CSL for The Company of Biologists.

  2. Create an .Rmd file with the following content.

---
title: "bibliographyquestion"
output: 
    bookdown::word_document2
bibliography: references.bib
csl: "the-company-of-biologists.csl"
---

[@R-pracma]
  1. Knit the .Rmd file

The output Word document looks like this:

My question is, how do I get the version number and URL in there? They're in the note and url fields of the BibTeX entry, respectively. I've tried editing the CSL to support type Manual, but it looks like the BibTeX entries get edited during an intermediate step because @Manual{...} entries (like R packages) get picked up by the book entry type.

Right now I have to manually edit my bibliography's R package entries every time I knit a new version, but I'm sure there has to be a better way. Thanks in advance for your help!

citation("dplyr")
#> 
#> To cite package 'dplyr' in publications use:
#> 
#>   Hadley Wickham, Romain François, Lionel Henry and Kirill Müller
#>   (2021). dplyr: A Grammar of Data Manipulation. R package version
#>   1.0.7. https://CRAN.R-project.org/package=dplyr
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {dplyr: A Grammar of Data Manipulation},
#>     author = {Hadley Wickham and Romain François and Lionel Henry and Kirill Müller},
#>     year = {2021},
#>     note = {R package version 1.0.7},
#>     url = {https://CRAN.R-project.org/package=dplyr},
#>   }

Come back with a new question if you need help doing it in batches.

Yes, I can get the BibTeX information from the citation() function. The question is: how do I get the information in the note and url fields to show up in the bibliography when I knit the .Rmd file?

Be more specific about how you're doing this now, with code?

And I should have just said

``` r
entry <- toBibtex(citation("dplyr"))
entry
#> @Manual{,
#>   title = {dplyr: A Grammar of Data Manipulation},
#>   author = {Hadley Wickham and Romain François and Lionel Henry and Kirill Müller},
#>   year = {2021},
#>   note = {R package version 1.0.7},
#>   url = {https://CRAN.R-project.org/package=dplyr},
#> }

Yes, I think you are heading in the right direction with editing the CSL file to get the package version number to show in your bibliography. In the bibtex entry the pkg version number is in the 'note' field, which most citation styles do not include when generating the bibliography.

Here's how I edited the-company-of-biologists.csl file to make the note field show in the bibliography. I opened the file in a text editor, then among the lines that define macros, say around line 57, I added this:

  <macro name="note">
    <text variable="note"/>
  </macro>

then further down where I saw the item title being set, I added this line:

<text macro="note" />

in this group of lines, to show the context of where exactly I added that:

          <group delimiter=" " prefix=" ">
            <text macro="title" font-style="italic" suffix="."/>
            <text macro="note" />
            <text macro="edition"/>
            <text macro="editor"/>
            <text macro="publisher"/>
          </group>

And when I knit my Rmd with this modified CSL, I get this in the reference list:

Borchers, H. W. (2021). Pracma: Practical numerical math functions. R package version 2.3.3.

Is that what you are after? Of course it will print the note field for all items in the bibliography, so some editing of the bib file might be required to remove unwanted note text in journal articles etc. Perhaps there is some logic that can be added to the csl file so that notes are only printed for Manual type items? In any case, let me know how that works for you!

1 Like

What and where is shown in bibliography, depends on .cslstyle. @benmarwick put a light on it. Either you would change used style, either you have to edit the used one.

Regards,
Grzegorz

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.