Printing UTF-8 characters (Russian) in R, Rmd, knitr

My Rmd / R file is saved with UTF-8 encoding. Other sessionInfo() :

 Platform: x86_64-w64-mingw32/x64 (64-bit) LC_CTYPE=English_Canada.1252 other attached packages: [1] knitr_1.17 

Here is a simple data frame that I need to print as a table in an html document, for example. using kable(dt) or any other way.

 dt <- data.frame( name=c(" ","Martin Luter King"), year=c("2015","1968") ) 

None of the following works:

Method 1

If I save Sys.setlocale () as it is (i.e. "English_Canada.1252" ), I get the following:

 > dt; name year 1 <U+0411><U+043E><U+0440><U+0438><U+0441> <U+041D><U+0435><U+043C><U+0446><U+043E><U+0432> 2015 2 Martin Luter King 1968 > kable(dt) |name |year | |:-----------------------------------------------------------------------------------------|:----| |<U+0411><U+043E><U+0440><U+0438><U+0441> <U+041D><U+0435><U+043C><U+0446><U+043E><U+0432> |2015 | |Martin Luter King |1968 | 

Note that <U+....> printed instead of characters.
Using dt$name <- enc2utf8(as.character(dt$name)) did not help.

Way 2

If I change Sys.setlocale("LC_CTYPE", "russian") # "Russian_Russia.1251", then I get the following:

 > dt; name year 1 Áîðèñ Íåìöîâ 2015 2 Martin Luter King 1968 > kable(dt) |name |year | |:-----------------|:----| |Áîðèñ Íåìöîâ |2015 | |Martin Luter King |1968 | 

Note that the characters have become gibberish.
Using print(dt,encoding="windows-1251"); print(dt,encoding="UTF-8") print(dt,encoding="windows-1251"); print(dt,encoding="UTF-8") had no effect.

Any tips?

The closest I could find to solve this problem are in the following links, but they did not help: http://blog.rolffredheim.com/2013/01/r-and-foreign-characters.html , https: // tomizonor .wordpress.com / 2013/04/17 / file-utf8-windows , https://www.smashingmagazine.com/2012/06/all-about-unicode-utf8-character-sets

I also tried to save the file with 1251 encoding (instead of the current UTF-8 encoding) and some other character conversion / processing packages. So far nothing has helped.

UPDATE:

Open related question: How to change Sys.setlocale when you receive the error message "request to install the language ... cannot be respected and

+1
source share
1 answer

The only solution that worked was proposed by Yihui Xie ( knitr developer), which: by creating a .Rprofile file that contains one line of Sys.setlocale("LC_CTYPE", "russian") and puts it in your home or working directory.

However, kable() that it only works using kable() , i.e. using the knitr package.
If you try to print using print(dt$name[1]) , you will still get Áîðèñ Íåìöîâ .
However, if you use kable(dt$name[1]) , you will get what you need - !

0
source

All Articles