14

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!

user2230101
  • 455
  • 3
  • 6
  • 15

2 Answers2

27

More info : https://stat.ethz.ch/R-manual/R-devel/library/base/html/locales.html

You'll get all your local variables through: sessionInfo()

Exemple From R doc :

Sys.getlocale()
Sys.getlocale("LC_TIME")

Set your language

Windows

  • Sys.setlocale("LC_TIME", "German")
  • Sys.setlocale("LC_TIME", "English")
  • Sys.setlocale("LC_TIME", "French")

Other

  • Sys.setlocale("LC_TIME", "de") # Solaris: details are OS-dependent
  • Sys.setlocale("LC_TIME", "de_DE") # Many Unix-alikes
  • Sys.setlocale("LC_TIME", "de_DE.UTF-8") # Linux, macOS, other Unix-alikes
  • Sys.setlocale("LC_TIME", "de_DE.utf8") # some Linux versions
bathyscapher
  • 1,615
  • 1
  • 13
  • 18
Delje
  • 271
  • 3
  • 3
  • On Linux, you will likely also need to do `sudo locale-gen ru_RU.UTF-8; sudo update-locale` to make a locale available at the OS level. – Alpi Murányi May 02 '21 at 23:40
16

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.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248