[R] Creating bibtex file of all installed packages?

Michael Friendly friendly at yorku.ca
Wed Dec 16 00:03:49 CET 2009


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?

Rpackage.bibs <- function(file="Rpackages.bib") {
   pkgs <-installed.packages()[,1]
   bibs <- lapply(pkgs, function(x) try(toBibtex(citation(x))))
   n.installed <- length(bibs)

   ## omit failed citation calls
   #bibs <- bibs[-which(sapply(bibs, class) == "try-error")]
   bibs <- bibs[!(sapply(bibs, class) == "try-error")]
   n.converted <- length(bibs)
   ## unify to list of Bibtex
   bibs <- lapply(bibs, function(x) if(inherits(x, "Bibtex")) list(x) 
else x)

   ## add bibtex keys to each entry
   ## -- change the first item of each bib entry from '@Manual{,' to 
'@Manual{pkgname,'

   ## list of unique entries
   bibs <- unique(do.call("c", bibs))
   ## write everything to a single .bib file
   writeLines(do.call("c", lapply(bibs, as.character)), file)
   cat("Converted ", n.converted, " of ", n.installed, " package 
citations to BibTex\n",
       "Results written to file ", file, "\n")
}



Achim Zeileis wrote:
> On Fri, 11 Dec 2009, Rainer M Krug wrote:
> 
>> Hi
>>
>> is there an easy and fast way, to generate a BibTeX file of all 
>> installed /
>> loaded packages and R?
>>
>> I know about toBibtex(citation()) to extract the BibTeX for a single
>> package, but how can I generate a file containg citations for all 
>> installed
>> / loaded packages?
> 
> I don't think that there is a way other than calling citation() for each 
> of the installed.packages(). You could do something like this:
> 
> ## try to get BibTeX for each of the installed packages
> b <- lapply(installed.packages()[,1], function(x)
>   try(toBibtex(citation(x))))
> ## omit failed citation calls
> b <- b[-which(sapply(b, class) == "try-error")]
> ## unify to list of Bibtex
> b <- lapply(b, function(x) if(inherits(x, "Bibtex")) list(x) else x)
> ## list of unique entries
> b <- unique(do.call("c", b))
> ## write everything to a single .bib file
> writeLines(do.call("c", lapply(b, as.character)), "Rpackages.bib")
> 
> hth,
> Z

-- 
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    http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT  M3J 1P3 CANADA




More information about the R-help mailing list