R, Rstudio Console Encoding Windows

2019-05-15 13:53发布

问题:

I there a way to change the console encoding in Rstudio on windows?

This is not about reading files or sourcing scripts in a specific encoding but about changing the console encoding (the encoding Sys.getlocale yields).

This is usually not a big problem, but i try to create a package and I'm using german umlauts in some strings and symbols. roxygen2 works best with UTF-8 encoding but covr gives an error if the source files are not in the console/system encoding.

(If there's a workaround for the roxygen2 and covr problem this would also help. Addintional info: I call covr::package_coverage from the console and I call roxygen2 via Rstudios Build command.)

and sessioninfo:

sessionInfo()
# R version 3.4.2 (2017-09-28)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows >= 8 x64 (build 9200)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=German_Austria.1252  LC_CTYPE=German_Austria.1252    LC_MONETARY=German_Austria.1252 LC_NUMERIC=C                    LC_TIME=German_Austria.1252    

Update: Sourcing the function that gives an error through the file in Rstudio works, loading with devtools::load_all(".") and the RStudio gui gives the error/wrong encoding.

回答1:

If you're asking how to change the locale to a UTF-8-capable one on Windows, the answer is you can't.

When you call Sys.setlocale, R will call the setlocale system function. Unfortunately, there isn't a way to specify UTF-8 for the character type (LC_CTYPE); see https://stackoverflow.com/a/4336010/6233565 .

What you can do is change the locale to one of those described at https://msdn.microsoft.com/library/windows/desktop/dd373814.aspx . For example, to change to Windows-1251, do

Sys.setlocale("LC_CTYPE", ".1251")

Hopefully, at some point in the future you will be able to write UTF-8 encoded data on Windows. Currently, that's not possible. All output goes through the native locale: https://stat.ethz.ch/pipermail/r-devel/2017-June/074556.html