Change system locale permanently
You could create or edit .Rprofile
, at user's level, or at project's level, see.
For all R sessions (and hence RStudio sessions), this should work:
file.edit(file.path("~", ".Rprofile"))
add in .Rprofile
:
Sys.setlocale("LC_ALL","English")
How to change language settings in R
You can set this using the Sys.setenv()
function. My R session defaults to English, so I'll set it to French and then back again:
> Sys.setenv(LANG = "fr")
> 2 + x
Erreur : objet 'x' introuvable
> Sys.setenv(LANG = "en")
> 2 + x
Error: object 'x' not found
A list of the abbreviations can be found here.
Sys.getenv()
gives you a list of all the environment variables that are set.
change time locale for R
For Windows:
Sys.setlocale("LC_TIME", "English")
Best practice: Should I try to change to UTF-8 as locale or is it safe to leave it as is?
This is not a perfect answer but a good workaround: As Roland pointed out, it might be dangerous to change the locale. So leave it as is. If you have a file and you run into trouble, just search for non-UTF8 encoding as discribed here for RStudio
. What I saw, most Editors have such a feature.
Furthermore, this answer gives more insight in what you can do in case you source()
a file.
For a way to deal with locales when collations play a crucial part see here
How to set the default language of date in R
OK, as a Q&A site, it seems an answer is required. From your description, it appears to be the issue of your locales. Read ?locales
for more.
You can have a test with this (read ?strptime
for various format, and pay special attention to those sensitive to locales):
format(Sys.Date(), format = "%Y-%b-%d")
# [1] "2016- 9月-06"
The output has the month in Chinese. If I want to change the display, I need to set "LC_TIME"
locale to "C"
:
Sys.setlocale("LC_TIME", "C")
Then it is OK:
format(Sys.Date(), "%Y-%b-%d")
# [1] "2016-Sep-06"
Every time you start a new R session, you get back to native setting. Should you want a permanent change, put
.First <- function() {
Sys.setlocale("LC_TIME", "C")
}
in the $(R RHOME)/etc/Rprofile.site
file. Read ?Startup
for how to customize R startup and the use of .First
.
Set locale to system default UTF-8
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 +0000
LANG="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.
Related Topics
How to Deal With "Package 'Xxx' Is Not Available (For R Version X.Y.Z)" Warning
Split Data Frame String Column into Multiple Columns
Select the Row With the Maximum Value in Each Group
Count Number of Rows Within Each Group
Quickly Reading Very Large Tables as Dataframes
How to Find the Statistical Mode
Use Dynamic Name For New Column/Variable in 'Dplyr'
Split Delimited Strings in a Column and Insert as New Rows
Find Complement of a Data Frame (Anti - Join)
Reshape Three Column Data Frame to Matrix ("Long" to "Wide" Format)
How to Implement Coalesce Efficiently in R
R Reshape Data Frame from Long to Wide Format
Sample Random Rows in Dataframe
How to Delete a Row by Reference in Data.Table
How to Plot All the Columns of a Data Frame in R
Mapping Columns/Rows from One Dataframe to Another Based on Row Number