From georgi@bo@hn@kov @ending from m@nche@ter@@c@uk  Sun May 13 10:24:50 2018
From: georgi@bo@hn@kov @ending from m@nche@ter@@c@uk (Georgi Boshnakov)
Date: Sun, 13 May 2018 08:24:50 +0000
Subject: [Rd] BUG: 'bibentry' methods change default bibstyle
Message-ID: <438D2EC9EAFE5946B2D5864670EA468E018F659BDB@MBXP01.ds.man.ac.uk>

Hi,

Some 'bibentry' methods have the side effect of changing the default bibstyle.

Background:
tools::bibstyle() defines and registers bibtex styles for formatting 'bibentry' objects.
It optionally (and by default) sets a default style.  It is also used to get a particular style.

The last feature of bibstyle() is used in 'bibentry' methods defined  in packages 'tools' and 'utils' , see the excerpts below.
They have the side effect of resetting the default bibstyle. This seems like a bug. I t can be resolved by adding argument '.default = FALSE' to the calls of bibstyle() in the bibentry methods for sort() and toRd() (there maybe other methods using this and perhaps also unexported functions).

Here is the example from the help page of bibstyle():

======================
   refs <-
     c(bibentry(bibtype = "manual",
         title = "R: A Language and Environment for Statistical Computing",
         author = person("R Core Team"),
         organization = "R Foundation for Statistical Computing",
         address = "Vienna, Austria",
         year = 2013,
         url = "https://www.R-project.org"),
       bibentry(bibtype = "article",
         author = c(person(c("George", "E.", "P."), "Box"),
                    person(c("David",  "R."),      "Cox")),
         year = 1964,
         title = "An Analysis of Transformations",
         journal = "Journal of the Royal Statistical Society, Series B",
         volume = 26,
         pages = "211-252"))

     bibstyle("unsorted", sortKeys = function(refs) seq_along(refs),
         fmtPrefix = function(paper) paste0("[", paper$.index, "]"),
            .init = TRUE)

=======================

As expected, the bibstyle() call sets "unsorted" as default bibstyle but here is how calls to sort() and toRd() have the side effect of changing the default style:

> tools::getBibstyle()
[1] "unsorted"
> sort(refs, .bibstyle = "JSS")
> tools::getBibstyle()
[1] "JSS"
> tools::toRd(refs, style = "unsorted")
> tools::getBibstyle()
[1] "unsorted"
>
========================

The same happens with format() and print() but this seems due to their use of sort().

Excerpts from bibentry methods:
==================================

> utils:::sort.bibentry
function (x, decreasing = FALSE, .bibstyle = NULL, drop = FALSE,
    ...)
{
    x[order(tools::bibstyle(.bibstyle)$sortKeys(x), decreasing = decreasing),
        drop = drop]
}

> tools:::toRd.bibentry
function (obj, style = NULL, ...)
{
    obj <- sort(obj, .bibstyle = style)
    style <- bibstyle(style)

   ...

}

======================


Georgi Boshnakov


	[[alternative HTML version deleted]]



[Rd] BUG: 'bibentry' methods change default bibstyle
Georgi Boshnakov