R或RStudio无法识别中文字符

时间:2017-04-03 16:53:01

标签: r rstudio

我把它保存为RStudio中的脚本:

a <- "中文"
print(a)

当我运行它时,我收到此错误:

Error in source("C:/Users/Rafael/Desktop/test.R", encoding = "UTF-8",  : 
  C:/Users/Rafael/Desktop/test.R:21:6: unexpected INCOMPLETE_STRING
20: 
21: a <- "
         ^
In addition: Warning message:
In readLines(file, warn = FALSE) :
  invalid input found on input connection 'C:/Users/Rafael/Desktop/test.R'

这是sessionInfo():

R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

loaded via a namespace (and not attached):
[1] tools_3.3.3

编辑:这只发生在我使用RStudio上的Source按钮运行时,如果我突出显示文本块并选择Run,则问题不会持续存在。

1 个答案:

答案 0 :(得分:3)

这似乎是Windows / UTF-8编码问题。如果您使用eval(parse('test.R', encoding = 'UTF-8'))而不是source(),则可以使用。