我试图让Eclipse的控制台使用波兰语(或任何其他非英语)字符,无论是ANSI还是UTF-8编码 . 似乎在Windows上,R只能用ANSI编码,而Eclipse的控制台强制使用UTF-8或ISO-8859-1 .

尝试使用ANSI CP-1250(Windows的默认波兰语编码),I:

  • 将R脚本文件编码为ANSI CP-1250

  • 设置Eclipse属性,包括"R Script File"内容类型(一般 - >内容类型 - >文本),"Text file encoding"(一般 - >工作区),控制台编码(在"Run Configurations" - > "R Console" - > "Common",如cp1250

  • 通过添加行在eclipse.ini中设置JVM属性:"-Dclient.encoding.override=cp1250","-Dfile.encoding=cp1250"

完全没有效果 . How may I force Eclipse to encode and display in R's locale?

当所有这些选项都设置为“UTF-8”而不是“CP-1250”时,完全相同的行为仍然存在 . 请注意我无法在Windows上将R的区域设置为“UTF-8” . 值得一提的是Rstudio,Rgui和Rterm不会对默认的CP-1250编码造成任何问题,字符串会正确显示 .

Executed script:

print(Sys.getlocale())
Sys.setenv(LANG = 'pl_PL.cp1250')

x <- 'ąęłóżść'
message('Printing variable'); print(x); print(charToRaw(x))

Output 1: 'Run via source' - >使用ANSI CP1250编码的字符串,但打印为ISO-8859-1

> source("C:/mjktfw/pit/workspace/test_encoding/run3.R", echo=FALSE, encoding="cp1250")
[1] "LC_COLLATE=Polish_Poland.1250;LC_CTYPE=Polish_Poland.1250;LC_MONETARY=Polish_Poland.1250;LC_NUMERIC=C;LC_TIME=Polish_Poland.1250"
Printing variable
[1] "¹ê³ó
[1] b9 ea b3 f3 bf 9c e6

Output 2: 'Run via submitting directly' - >用UTF-8编码的字符串,正确打印

> print(Sys.getlocale())
[1] "LC_COLLATE=Polish_Poland.1250;LC_CTYPE=Polish_Poland.1250;LC_MONETARY=Polish_Poland.1250;LC_NUMERIC=C;LC_TIME=Polish_Poland.1250"
> Sys.setenv(LANG = 'pl_PL.cp1250')
> 
> x <- 'ąęłóżść'
> message('Printing variable'); print(x); print(charToRaw(x))
Printing variable
[1] "ąęłóżść"
 [1] c4 85 c4 99 c5 82 c3 b3 c5 bc c5 9b c4 87

Output 3: copy-paste to console: - >用UTF-8编码的字符串,正确打印

> print(Sys.getlocale())
[1] "LC_COLLATE=Polish_Poland.1250;LC_CTYPE=Polish_Poland.1250;LC_MONETARY=Polish_Poland.1250;LC_NUMERIC=C;LC_TIME=Polish_Poland.1250"
> Sys.setenv(LANG = 'pl_PL.cp1250')
> 
> x <- 'ąęłóżść'
> message('Printing variable'); print(x); print(charToRaw(x))
Printing variable
[1] "ąęłóżść"
 [1] c4 85 c4 99 c5 82 c3 b3 c5 bc c5 9b c4 87

Output 4: 'Run entire command in R'快捷方式 - >字符串编码为ANSI CP1250,但打印为明文Unicode代码点

> print(Sys.getlocale())
[1] "LC_COLLATE=Polish_Poland.1250;LC_CTYPE=Polish_Poland.1250;LC_MONETARY=Polish_Poland.1250;LC_NUMERIC=C;LC_TIME=Polish_Poland.1250"
> Sys.setenv(LANG = 'pl_PL.cp1250')
> x <- 'ąęłóżść'
> message('Printing variable')
Printing variable
> print(x)
[1] "<U+00B9><ea><U+00B3><f3><U+00BF><U+009C><e6>"
> print(charToRaw(x))
[1] b9 ea b3 f3 bf 9c e6

编辑

经过一些修改之后,上述情况会由字符串的不同'raw'编码和 Encoding()<- 参数产生 . 下面的输出比较了R / Rstudio和Eclipse / StatET行为:

Eclipse

> # UTF-8 encoded string
> char <- rawToChar(as.raw(c(0xea, 0xb3, 0x9c)))
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: 곜"
[1] "Encoding: unknown | Raw: ea b3 9c"
> 
> Encoding(char) <- 'UTF-8'
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: <U+ACDC>"
[1] "Encoding: UTF-8 | Raw: ea b3 9c"
> 
> # ANSI encoded string
> char <- rawToChar(as.raw(c(0xc4, 0x99, 0xc5, 0x82, 0xc5, 0x9b)))
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: ęłś"
[1] "Encoding: unknown | Raw: c4 99 c5 82 c5 9b"
> 
> Encoding(char) <- 'UTF-8'
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: 곜"
[1] "Encoding: UTF-8 | Raw: c4 99 c5 82 c5 9b"

Rstudio

> # UTF-8 encoded string
> char <- rawToChar(as.raw(c(0xea, 0xb3, 0x9c)))
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: ęłś"
[1] "Encoding: unknown | Raw: ea b3 9c"
> 
> Encoding(char) <- 'UTF-8'
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: 곜"
[1] "Encoding: UTF-8 | Raw: ea b3 9c"
> 
> # ANSI encoded string
> char <- rawToChar(as.raw(c(0xc4, 0x99, 0xc5, 0x82, 0xc5, 0x9b)))
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: ęłś"
[1] "Encoding: unknown | Raw: c4 99 c5 82 c5 9b"
> 
> Encoding(char) <- 'UTF-8'
> sprintf('String: %s', char); sprintf('Encoding: %s | Raw: %s', Encoding(char), paste(charToRaw(char), collapse = ' '))
[1] "String: ęłś"
[1] "Encoding: UTF-8 | Raw: c4 99 c5 82 c5 9b"