[R] Creating bibtex file of all installed packages?
Romain Francois
romain.francois at dbmail.com
Wed Dec 16 08:48:21 CET 2009
On 12/16/2009 08:32 AM, Achim Zeileis wrote:
>
> 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
And you can read it back into R with package bibtex:
> require( bibtex )
> entries <- read.bib( "Rpackages.bib" )
but then although the key is read, it is not displayed, because of
utils:::toBibtex.citation:
> entries[[1]]
A BibTeX entry for LaTeX users is
@Manual{,
title = {ant: Version of ant specific to R},
author = {Romain Francois},
year = {2009},
note = {R package version 0.0-10},
}
> str( entries[[1]] )
List of 4
$ title : chr "ant: Version of ant specific to R"
$ author:List of 1
..$ :List of 2
.. ..$ name : Named chr [1:3] "Romain" "" "Francois"
.. .. ..- attr(*, "names")= chr [1:3] "first" "middle" "last"
.. ..$ email: NULL
.. ..- attr(*, "class")= chr "person"
..- attr(*, "class")= chr "personList"
$ year : chr "2009"
$ note : chr "R package version 0.0-10"
- attr(*, "class")= chr "citation"
- attr(*, "srcref")=Class 'srcref' atomic [1:6] 1 1 6 1 1 1
.. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x8ed067c>
- attr(*, "entry")= chr "Manual"
- attr(*, "key")= chr "ant
Should I submit some patch to allow printing of the key when there is a
key attribute. Maybe the key could also be used for character based
indexing of citationList objects.
Romain
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/HlX9 : new package : bibtex
|- http://tr.im/Gq7i : ohloh
`- http://tr.im/FtUu : new package : highlight
More information about the R-help
mailing list