Re: [Rd] R package BibTex entries: looking for a more general solution

From: Achim Zeileis <Achim.Zeileis_at_uibk.ac.at>
Date: Fri, 16 Dec 2011 14:51:01 +0000 (GMT)

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:

> Back in 2010 I raised this issue, and there was some discussion,
>
> https://stat.ethz.ch/pipermail/r-devel/2010-November/058987.html
>
> The goal, then, as now is to have a way to produce a bibtex-clean .bib file
> (i.e., not requiring
> manual editing except in unusual circumstances) reflecting installed
> packages
> for use in writing where one often needs/wants to cite all packages used in
> a given article.
>
> Achim wrote the function below  which largely does this job, quite nicely
> now that most
> DESCRIPTION files now contain Authors_at_R fields.  However, something changed
> since
> R 2.11.1 when I tried this last, so that the function no longer generates
> keys for packages
> which contain more than one citation.
>
> I've tried debugging with browser(), but can't figure out how to make the
> lines around FIXME
> work.  Can someone help?
>
> You can see the result from this at
> http://euclid.psych.yorku.ca/SCS/Private/Rbibs/Rpackages-2.14.0.bib
> The function is also at:
>
> # Original code by Achim Zeileis, 16 Dec 2009, R-help
> # Added: support header and preamble
>
> Rpackages.bib <- function(filename = paste("Rpackages-",getRversion(),
> ".bib", sep=""),
>         header=TRUE, preamble=NULL, suppress.warnings=TRUE, verbose = TRUE)
> {
>   ## installed packages
>   pkgs <- unique(installed.packages()[,1])
>   if (suppress.warnings) warn <- options(warn=-1)
>   bibs <- lapply(pkgs, function(x) try(toBibtex(citation(x))))
>   if (suppress.warnings) options(warn)
>  
>   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)
>
>   ## FIXME: add bibtex keys to each entry [the line below does not work!!]
>   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)
>
>     if(header) header <- gsub("^", "%", toLatex(sessionInfo()))
>     output <- file(filename, "a")
>     cat(header, preamble, sep='\n', file=output, append=TRUE)
>   ## write everything to a single .bib file
>   writeLines(do.call("c", lapply(bibs, as.character)), con=output)
>   close(output)
>   if(verbose) cat("Converted", n.converted, "of", n.installed,
>     "package citations to BibTeX",
>     "\nResults written to file", filename, "\n")
>
>   ## return Bibtex items invisibly
>   invisible(bibs)
> }
>
>
>
>
>
>
> --
> 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 Web: http://www.datavis.ca
> Toronto, ONT M3J 1P3 CANADA
>
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 16 Dec 2011 - 14:57:40 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 16 Dec 2011 - 22:20:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive