[R] Creating bibtex file of all installed packages?
Romain Francois
romain.francois at dbmail.com
Fri Dec 11 09:02:51 CET 2009
On 12/11/2009 08:41 AM, 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")]
I would use logical indexing instead of "which" because if none actually
fail, you end up indexing by integer(0) so b is empty.
b <- b[!(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
If you then want to do the reversed operation, read a bibtex file into a
"citationList" object, you can use the unreleased(*) package bibtex.
> install.packages("bibtex", repos="http://R-Forge.R-project.org")
> require( bibtex )
Loading required package: bibtex
> bib <- read.bib( "Rpackages.bib" )
There were 50 or more warnings (use warnings() to see the first 50)
(*) because I have been lazy
The warnings are all about the lack of keys in the entries cooked by
toBibtex. no big deal.
Romain
>> Cheers,
>>
>> Rainer
>>
>> --
>> NEW GERMAN FAX NUMBER!!!
>>
>> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
>> Biology,
>> UCT), Dipl. Phys. (Germany)
>>
>> Centre of Excellence for Invasion Biology
>> Natural Sciences Building
>> Office Suite 2039
>> Stellenbosch University
>> Main Campus, Merriman Avenue
>> Stellenbosch
>> South Africa
>>
>> Cell: +27 - (0)83 9479 042
>> Fax: +27 - (0)86 516 2782
>> Fax: +49 - (0)321 2125 2244
>> email: Rainer at krugs.de
>>
>> Skype: RMkrug
>> Google: R.M.Krug at gmail.com
>>
>> [[alternative HTML version deleted]]
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/Gq7i : ohloh
|- http://tr.im/FtUu : new package : highlight
`- http://tr.im/EAD5 : LondonR slides
More information about the R-help
mailing list