10

My system: win7 ultimate 64 english version + r-3.1(64) .
Here is my sessionInfo.

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252     
LC_MONETARY=English_United States.1252 LC_NUMERIC=C      
LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

1.can't input chinese character into r console
When I input a chinese character in r console, it turns to garbled character .

enter image description here enter image description here

2.can't display chinese character on the r console
When I read data in r console, the chinese character turns into a garbled character .
You can download the data, and test it with

read.table("r1.csv",sep=",")

Download Data

enter image description here

Please see the graph to download the data if you don't know how to get the data from my web.

enter image description here

How can I setup my pc to properly display and input chinese characters in r console? I have updated the chinese language pack ,and enabled it,but problem remains still.

showkey
  • 449
  • 30
  • 101
  • 235

1 Answers1

5

It is probably not very well documented, but you want to use setlocale in order to use Chinese. And the method applies to many other languages as well. The solution is not obvious as the official document of setlocale didn't specifically mentioned it as a method to solve the display issues.

> print('ÊÔÊÔ') #试试, meaning let's give it a shot in Chinese
[1] "ÊÔÊÔ" #won't show up correctly
> Sys.getlocale()
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
> Sys.setlocale(category = "LC_ALL", locale = "chs") #cht for traditional Chinese, etc.
[1] "LC_COLLATE=Chinese_People's Republic of China.936;LC_CTYPE=Chinese_People's Republic of China.936;LC_MONETARY=Chinese_People's Republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese_People's Republic of China.936"
> print('试试')
[1] "试试"
> read.table("c:/CHS.txt",sep=" ") #Chinese: the 1st record/observation
  V1   V2  V3 V4  V5   V6
1 122 第一 122 条 122 记录 

If you just want to change the display encoding, without changing other aspects of locales, use LC_CTYPE instead of LC_ALL:

> Sys.setlocale(category = "LC_CTYPE", locale = "chs")
[1] "Chinese_People's Republic of China.936"
> print('试试')
[1] "试试"

Now, of course this only applies to the official R console. If you use other IDE's, such as the very popular RStudio, you don't need to do this at all to be able to type and display Chinese, even if you didn't have the Chinese locale loaded.

Migrate some useful stuff from the following comments:

If the data still fails to show up correctly, the we should also look into the issue of the file encoding. If the file is UTF-8 encoded, tither data <- read.table("you_file", sep=',', fileEncoding="UTF-8-BOM", header=TRUE) or fileEncoding="UTF-8" will do, depends on which encoding it really has.

But you may want to stay away from UTF-BOM as it is not recommended: What's different between UTF-8 and UTF-8 without BOM?

Community
  • 1
  • 1
CT Zhu
  • 44,804
  • 14
  • 99
  • 113
  • can i wirte it into a configuration file? – showkey Jul 02 '14 at 05:13
  • Yep, it is called **startup file**, see this: http://www.statmethods.net/interface/customizing.html – CT Zhu Jul 02 '14 at 06:24
  • With your permission. May I suggest change the title to `...chinese (and other non-ASCII) character in r console?`, the issue is not really Chinese specific and others may benefit from it as well. – CT Zhu Jul 02 '14 at 06:31
  • a last problem about the post : if the data file (http://pan.baidu.com/s/1hqklUfU) is in the form of ansi,chinese character can be displayed properly,if data file is saved as the form of utf, same read.table command can not display the chinese character ,it is a garbled character. please resave the data file in the form of utf-8 with notepad,and try `read.table("r1.csv",sep=",")`.how can i do then if the data file is utf8 form? – showkey Jul 02 '14 at 07:16
  • You can change all the data files with notepad in the form of ansi,and open it in r with read.table, is there a another way to do ? – showkey Jul 02 '14 at 07:19
  • No, you don't have to. Try either `data – CT Zhu Jul 02 '14 at 16:17
  • I tried set locale but it does not work. I have a chinese win 10 and chinese r.base. still, not print chinese when it is in R studio – News_is_Selection_Bias Jul 26 '16 at 03:48