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

Alex Deckmyn alex.deckmyn at meteo.be
Fri Sep 18 22:25:21 CEST 2015


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]]



More information about the R-package-devel mailing list