Rstudio Version 1.0.136
R Version 3.3.2
It's strange that when I run code(it has Chinese comment in code)line by line in a .Rmd
file with Rmarkdown,console will print a warning as follow:
Warning message: In strsplit(code, "\n", fixed = TRUE) : input string 1 is invalid in this locale
It's so annoying ,because every line it will appear. I has change default text encoding in RStudio's setting ,but neither UTF-8 nor BG2312 can prevent this warning messag appearing. Please notice that it just appear when a run code line by line ,if I select a chunk an press button to produce a html,warning doesn't appear. my code is as follows:
```{r} da=read.table("m-intcsp7309.txt",header=T) head(da) # date intel sp三列 length(da$date) # 444数据 intc=log(da$intc+1) # 测试 plot(cars) # 测试警告信息 plot(cars) # 为什么会出现警告? plot(cars) ```
I have test it's not arise from Chinese comment,I meet it when I only use English
just now. Here is more information:
Sys.getlocale() [1] "LC_COLLATE=Chinese (Simplified)_People's Republic of China.936; LC_CTYPE=Chinese (Simplified)_People's Republic of China.936; LC_MONETARY=Chinese (Simplified)_People's Republic of China.936; LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_People's Republic of China.936"
I had a similar issue with gsub()
and was able to resolve it, without changing the locale, simply by setting useBytes = TRUE
. The same should work in strsplit()
. From the documentation:
If TRUE the matching is done byte-by-byte rather than character-by-character, and inputs with marked encodings are not converted.
Embed this directly in the Rmarkdown script that contains the Chinese character comment(s):
Sys.setlocale('LC_ALL','C')
If you just run it in the R console before running the rmarkdown script, that may temporarily change the setting and work, but as you said, it won't stay that way if you restart R. That's why it's better to directly embed that line into the script(s) that need it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With