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

https://stat.ethz.ch/pipermail/r-devel/2010-November/058987.html

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@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
http://euclid.psych.yorku.ca/SCS/Private/Rbibs/Rpackages-2.14.0.bib
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 <- do.call("c", pkgs)
   bibs <- do.call("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(do.call("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
   invisible(bibs)
}






-- 
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:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA


	[[alternative HTML version deleted]]

