Set locale to system default UTF-8 Set locale to system default UTF-8 r r

Set locale to system default UTF-8


Try this:

Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")


Answering my own question: On Ubuntu the default LANG is defined in /etc/default/locale:

jeroen@dev:~⟫ cat /etc/default/locale# Created by cloud-init v. 0.7.7 on Wed, 29 Jun 2016 11:02:51 +0000LANG="en_US.UTF-8"

So in R we could do something like:

readRenviron("/etc/default/locale")LANG <- Sys.getenv("LANG")if(nchar(LANG))   Sys.setlocale("LC_ALL", LANG)

Apache also has a line in /etc/apache2/envvars that can be uncommented to enable this.


I guess you need to make a check for the OS. The locale names differ by OS, see the examples at the help file.

?Sys.getlocale()ExamplesSys.getlocale()Sys.getlocale("LC_TIME")## Not run: Sys.setlocale("LC_TIME", "de")     # Solaris: details are OS-dependentSys.setlocale("LC_TIME", "de_DE.utf8")   # Modern Linux etc.Sys.setlocale("LC_TIME", "de_DE.UTF-8")  # dittoSys.setlocale("LC_TIME", "de_DE")  # OS X, in UTF-8Sys.setlocale("LC_TIME", "German") # Windows## End(Not run)Sys.getlocale("LC_PAPER")          # may or may not be set## Not run: Sys.setlocale("LC_COLLATE", "C")   # turn off locale-specific sorting,                                   # usually, but not on all platforms## End(Not run)