Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Wrong encoding in Rstudio console (but ok in R GUI and ggplot2)

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

like image 227
RInatM Avatar asked Nov 11 '13 07:11

RInatM


2 Answers

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.

like image 140
ekotov Avatar answered Nov 05 '22 12:11

ekotov


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

fixed the problem in my case.

like image 30
Alex Knorre Avatar answered Nov 05 '22 12:11

Alex Knorre