user2230101 user2230101 - 1 year ago 52
R Question

How to set the default language of date in R

I used a R package written by other people. In the package, it is supposed to create a file name as "Mar.12". However the file name is "δΈ‰ζœˆ.12” in my system since I am running it on an OS with Chinese language (windows 10). I have changed the display language to English in the Rconsole file but it does not help. I am wondering is there any method to change the default date to English in R without revising the original package?

Thanks in advance for help!

Answer Source

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/ file. Read ?Startup for how to customize R startup and the use of .First.