[R-pkg-devel] problem with locale-dependent order()

Hadley Wickham h.wickham at gmail.com
Sun Sep 20 19:22:54 CEST 2015


Hi Alex,

As far as I know, that's the best solution. In roxygen, I've wrapped
it up with a couple of helpers:

set_collate <- function(locale) {
  cur <- Sys.getlocale(category = "LC_COLLATE")
  Sys.setlocale(category = "LC_COLLATE", locale = locale)
  cur
}

with_collate <- function(locale, code) {
  old <- set_collate(locale)
  on.exit(set_collate(old))

  force(code)
}

sort_c <- function(x) with_collate("C", sort(x))

Hadley

On Fri, Sep 18, 2015 at 4:25 PM, Alex Deckmyn <alex.deckmyn at meteo.be> wrote:
> Hi,
>
> For an update of the maps package, I have run into a bug caused by the locale-depent behaviour of order().
> In a certain function, the polygon names of the world map are lexically sorted with order() prior to being sent to a C routine. That routine expects them to be sorted according to ASCII rules, but that is not the result I get. The main culprit is the colon used to identify sub-regions. For instance, in my locale (en_GB_UTF-8), I get
>
> "uk:northen ireland" < "ukrain" < "uk:scotland"
>
> which is not the order expected by C. I guess some language settings would also cause other unexpected results.
>
> Apart from the pain of fixing uk/ukrain ambiguities, is there a safe way to fix this order() in a package? An obvious solution seems to be
>
> lcc <- Sys.getlocale("LC_COLLATE")
> Sys.setlocale("LC_COLLATE","C")
> ord.nam <- order(nam)
> Sys.setlocale("LC_COLLATE",lcc)
>
> This seems to work fine on my linux PC, but I am not sure about other platforms (Windows, OS-X...), though the "C" locale should be standard. Is this safe? Or is there a better way to get the right ordering?
>
> Alex
>
> ---
> Dr. Alex Deckmyn e-mail: alex.deckmyn at meteo.be
> Royal Meteorological Institute http://www.meteo.be
> Ringlaan 3, 1180 Ukkel, Belgium tel. (32)(2)3730646
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-package-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel



-- 
http://had.co.nz/



More information about the R-package-devel mailing list