Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Studio does not work with Chinese Characters properly

Tags:

r

encoding

It seems that I have problem working with Chinese characters in my R Studio. A simple codes like the following:

data <- c("物品","方案")
data
# [1] "\347\211\251\345\223\201" "\346\226\271\346\241\210"

It remains the same even if I run

Sys.setlocale(category="LC_ALL",locale="chinese")*

This happened to both my Windows laptop and Mac.

Can anyone tell me how to configure the R Studio in order to read the data in Chinese properly?

like image 856
Liu Qizhang Avatar asked Apr 06 '15 08:04

Liu Qizhang


1 Answers

Run in Shell R

> print("中文")
[1] "中文"
> Sys.getlocale()
[1] "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"

Run in RStudio

> print("中文")
[1] "\344\270\255\346\226\207"
> Sys.getlocale()
[1] "C"

By contrast, you can find the locale environment in RStudio is not set to support UTF-8.

You should not change the locale environment while RStudio is running, you can call Sys.setlocale in .Rprofile. (OS X is ok.)

$ cat ~/.Rprofile
Sys.setlocale(category="LC_ALL", locale = "en_US.UTF-8")

You can find more information in R help ?Startup and RStudio Character Encoding

like image 108
Loki Avatar answered Sep 28 '22 08:09

Loki