R Wrong encoding in Rstudio console (but ok in R G

2019-02-13 17:49发布

问题:

I'm on Windows 8.1 (en) and my R console won't understand Russian characters and produce smth like this (both in console and if I run R file)

> x <- "Привет"
> print(x)
[1] "Ïðèâåò"

I know that this can happen if you save CP1251 as CP1252. I set up all R options to UTF-8, and source files as UTF-8, but this doesn't help. I also set up sysLocale to Russian as here, but got nothing. Some advise from RStudio faq didn't help either. The strange thing though, that ggplot2 works absolutely fine

dt <- as.data.frame(cbind(x = c("Один", "Два"), y = c(3, 5)))
ggplot(dt, aes(x=x, y=y))+geom_bar() + xlab("Счет")

This is my sessionInfo()

R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Russian_Russia.1251  LC_CTYPE=Russian_Russia.1251    LC_MONETARY=Russian_Russia.1251 LC_NUMERIC=C                   
[5] LC_TIME=Russian_Russia.1251    

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

I use last version of RStudio, but dev version also would not help

UPDATE:

> Encoding(x)
[1] "unknown"
> getOption("encoding")
[1] "native.enc"

If I use RGUI, after Sys.setlocale("LC_ALL", "Russian") it will allow to

> print(x)
[1] "Привет"

I also checked in the Russian version of Windows - Rstudio works ok

回答1:

Sys.setlocale("LC_ALL", "Russian")

Did not work for me.

Sys.setlocale("LC_CTYPE", "en_RU.UTF-8")

Did the job! now utf files with Cyrillic characters display properly in the R/RStudio Console. But that only seems to work until R or RStudio is restarted.

also, running

defaults write org.R-project.R force.LANG en_US.UTF-8

in OS X Terminal solves all my problems with non-Latin characters for good.



回答2:

Sys.setlocale("LC_ALL", "Russian_Russia.1252")

fixed the problem in my case.