Re: [Rd] R package BibTex entries: looking for a more general solution

From: Michael Friendly <>
Date: Fri, 16 Dec 2011 08:46:29 -0500

Back in 2010 I raised this issue, and there was some discussion,

The goal, then, as now is to have a way to produce a bibtex-clean .bib file (i.e., not requiring
manual editing except in unusual circumstances) reflecting installed packages
for use in writing where one often needs/wants to cite all packages used in a given article.

Achim wrote the function below which largely does this job, quite nicely now that most
DESCRIPTION files now contain Authors_at_R fields. However, something changed since
R 2.11.1 when I tried this last, so that the function no longer generates keys for packages
which contain *more than one citation*.

I've tried debugging with browser(), but can't figure out how to make the lines around FIXME
work. Can someone help?

You can see the result from this at The function is also at:

# Original code by Achim Zeileis, 16 Dec 2009, R-help # Added: support header and preamble

Rpackages.bib <- function(filename = paste("Rpackages-",getRversion(), ".bib", sep=""),

         header=TRUE, preamble=NULL, suppress.warnings=TRUE, verbose = TRUE) {

## installed packages

   pkgs <- unique(installed.packages()[,1])    if (suppress.warnings) warn <- options(warn=-1)    bibs <- lapply(pkgs, function(x) try(toBibtex(citation(x))))    if (suppress.warnings) options(warn)

   n.installed <- length(bibs)

## omit failed citation calls

   ok <- !(sapply(bibs, class) == "try-error")    pkgs <- pkgs[ok]
   bibs <- bibs[ok]
   n.converted <- sum(ok)
## unify to list of Bibtex

   bibs <- lapply(bibs, function(x) if(inherits(x, "Bibtex")) list(x) else x)

## FIXME: add bibtex keys to each entry [the line below does not work!!]
   pkgs <- lapply(seq_along(pkgs), function(i) if(length(bibs[[i]]) > 1)      paste(pkgs[i], 1:length(bibs[[i]]), sep = "") else pkgs[i])    pkgs <-"c", pkgs)
   bibs <-"c", bibs)
   for(i in seq_along(pkgs)) bibs[[i]][1] <-      gsub("{,", paste("{", pkgs[i], ",", sep = ""), bibs[[i]][1], fixed = TRUE)

     if(header) header <- gsub("^", "%", toLatex(sessionInfo()))
     output <- file(filename, "a")
     cat(header, preamble, sep='\n', file=output, append=TRUE)

## write everything to a single .bib file
   writeLines("c", lapply(bibs, as.character)), con=output)    close(output)
   if(verbose) cat("Converted", n.converted, "of", n.installed,
     "package citations to BibTeX",
     "\nResults written to file", filename, "\n")

## return Bibtex items invisibly


Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:
Toronto, ONT  M3J 1P3 CANADA

	[[alternative HTML version deleted]]

______________________________________________ mailing list
Received on Fri 16 Dec 2011 - 13:50:55 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 19 Dec 2011 - 10:00:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive