On Tue, 15 Dec 2009, Michael Friendly wrote:

Achim and others:

Achim's solution could be directly usable if it also added a BibTeX key,
perhaps just the name of the package to the '@Manual{,' initial line of each. I wrapped the previous suggestions in a function, and played around with the components, but can't quite see how to account for the
failed citation calls.  Can anyone take the next step?

I had also thought about this after sending my previous solution, so here is an update Michael's version of the function (renamed to the name of the default file). Apart from some smaller touch-ups this has the following changes:

  - Instead of taking the unique() bibs, I now use the unique() pkgs.

    (In principle, packages with the same name could be installed in
    different libraries, potentially containing different citations.
    But I thought it would be overkill to check for that.)

  - Citation keys are simply "pkgname" if there is only a single
    BibTeX item, and "pkgname1" to "pkgnameN" if there are N BibTeX
    items.

    (This does not assure that citation keys are unique, though. If
    there is a package "foo" with 2 citation entries and another package
    "foo2" with only a single entry, these could be confused. A workaround
    would be to use "pkgname1" instead of "pkgname" as the citation key
    even if there is a single citation only. But I thought that would
    be less intuitive.)


Rpackages.bib <- function(file = "Rpackages.bib", verbose = TRUE)
{
  ## installed packages
  pkgs <- unique(installed.packages()[,1])
  bibs <- lapply(pkgs, function(x) try(toBibtex(citation(x))))
  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)

  ## add bibtex keys to each entry
  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)

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

  ## return Bibtex items invisibly
  invisible(bibs)
}

Best,
Z

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to