Skip to content
Prev 42081 / 63435 Next

R package BibTex entries: looking for a more general solution

Michael,

meanwhile the "bibentry" class that Kurt mentioned back in the discussion 
in 2010 is fully implemented in R. Hence the code can be simplified when 
working with the "bibentry" objects directly (instead of the "Bibtex" 
objects derived from them).

I've quickly hacked some code to illustrate how the function needs to be 
modified but I didn't have the time to integrate it into the function
(I'm currently traveling).

   ## query packages and their bibentries
? pkgs <- unique(installed.packages()[,1])
? bibs <- lapply(pkgs, function(x) try(citation(x)))

   ## exclude those with errors
? ok <- !(sapply(bibs, class) == "try-error")
? pkgs <- pkgs[ok]
? bibs <- bibs[ok]

   ## number of bibentries per package
   nref <- sapply(bibs, length)

   ## merge all bibentries
   bibs <- do.call("c", bibs)

   ## add citation keys
   bibkeys <- lapply(1:length(nref), function(i)
     if(nref[i] > 1) paste(pkgs[i], 1:nref[i], sep = ":") else pkgs[i])
   bibs$key <- as.list(unlist(bibkeys))

And then you just need to say toBibtex(bibs) or writeLines(toBibtex(bibs)) 
or something along those lines.

For more details on the new classes, see this recent working paper by Kurt 
Duncan and myself: http://epub.wu.ac.at/3269/.

So much for today.
Best wishes,
Z
On Fri, 16 Dec 2011, Michael Friendly wrote: