1

Packrat generates a lock file with all the packages actually used in a repository (including dependencies). I thought it'd be nice to use this lock file to generate my citations for the project website.

The lockfile has this format.

PackratFormat: 1.4
PackratVersion: 0.4.8.1
RVersion: 3.3.0
Repos: CRAN=https://cran.rstudio.com/

Package: BH
Source: CRAN
Version: 1.62.0-1
Hash: 14dfb3e8ffe20996118306ff4de1fab2

Package: DT
Source: CRAN
Version: 0.2
Hash: 36b032203797956fedad5a25055016a9
Requires: htmltools, htmlwidgets, magrittr

Etc. I'd like to end up with a bibtex file including citations to all packages + the R and the Packrat versions. Then I'd like to append this bibliography via Rmarkdown.

The first part can be done. I can kludge something with Regex matching, but I thought I should parse the file. There is an internal function in packrat that I will use for this, although internal is of course not optimal.

However, I'm unsure how to concatenate the citations afterwards and I also thought somebody might have done this before, hence this question. Simply concatenating them as a string might work, but they seem to lack identifiers and I need to mention them by an identifier in the markdown file..

Ruben
  • 3,452
  • 31
  • 47
  • Found [repmis](http://christophergandrud.github.io/repmis/) which does something similar, but it doesn't play with packrat yet. – Ruben Nov 23 '16 at 17:06

1 Answers1

1

I came up with the following. I'm not yet happy.
One ugly kludge is the inclusion of the package name as the bibtex identifier via str_replace.
Another is the problem that I need to generate the nocite string and manually paste it by hand into my .Rmd file.
Also, a lot of packages' citations don't include their precise version number, but locking to a specific version is the value added by packrat. I can still refer people to the lockfile for more info, but I might try adding this information to the Bibtex myself. And after looking at the long list of packages I've never seen before, I feel I need an option to limit it to packages I actually call myself.

library(stringr)
# use internal function to read lockfile (uses readDcf)
packages = packrat:::readLockFilePackages("packrat/packrat.lock")
package_names = names(packages) # get pkg names


getbib = function(...) { # small helper to extract citations
    # allow calling it with no argument to get R citation
    name = list(...)
    if (length(name) > 0) {
        name = name[[1]]
    } else {
        name = "R"
    }
    paste0(
        str_replace(
            as.character(toBibtex(citation(...))),
            # by default the bibtex entries dont have ids, I'm using the pkg name
            "\\{\\,", paste0( "{", name, ",")) ,
        collapse = "\n")
}

bibliography = paste0(c(
    getbib(), sapply(package_names, FUN = getbib)), # get R citation and all packages
    collapse = "\n\n")

# write bibliography to file
cat(bibliography, file = "packrat_bibliography.bibtex")

# generate YAML reference with nocite
cat(paste0("
bibliography: packrat_bibliography.bibtex
nocite: |
", paste0("@", c("R", package_names), collapse = " ")))
Ruben
  • 3,452
  • 31
  • 47