3

I have this saved as a script in RStudio:

a <- "中文"
print(a)

When I run it I get this error:

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'

This is the 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

Edit: This only happens when I run using the Source button on RStudio, if I highlight the block of text and select Run, then the problem does not persist.

M--
  • 18,939
  • 7
  • 44
  • 76
Rafael
  • 2,352
  • 1
  • 14
  • 47
  • That runs with no error on my machine. Try restarting R, and if that doesn't work, provide `sessionInfo()` – MichaelChirico Apr 03 '17 at 17:03
  • Added `sessionInfo()` – Rafael Apr 03 '17 at 17:07
  • I get the same error. If you run the two lines from the console, it works as expected. If you `source` it without specifying the encoding, it reads the file, runs, but prints `中文` instead. As far as I can tell, RStudio saves the script in `UTF-8`. Very odd. – Andrew Gustar Apr 03 '17 at 17:22
  • 2
    Please take a look at [here](https://support.rstudio.com/hc/en-us/community/posts/209786848-Chinese-support-in-Rstudio-on-Mac-OS) and [here](http://stackoverflow.com/questions/24473681/how-to-display-and-input-chinese-and-other-non-ascii-character-in-r-console) – M-- Apr 03 '17 at 17:25
  • 1
    Possible duplicate of [how to display and input chinese (and other non-ASCII) character in r console?](http://stackoverflow.com/questions/24473681/how-to-display-and-input-chinese-and-other-non-ascii-character-in-r-console) – M-- Apr 03 '17 at 17:28
  • I get no error using `source` either, even without specifying encoding. Neither in RStudio (Ctrl+Shift+S nor "Source" button) nor in terminal. – MichaelChirico Apr 03 '17 at 17:31
  • what happens if you run with `verbose = TRUE` – MichaelChirico Apr 03 '17 at 18:55
  • Added to the top of the file, no change – Rafael Apr 03 '17 at 19:21
  • Exactly facing the same problem here. I don't think any answer/comment here solves the issue. – jf328 Nov 08 '19 at 09:45

1 Answers1

4

This seems to be a Windows/UTF-8 encoding problem. It works if you use eval(parse('test.R', encoding = 'UTF-8')) instead of source().

Andrew Gustar
  • 14,365
  • 14
  • 26