Reputation: 333
I can read Chinese from R and see the Chinese words in RStudio. But I can not figure out how to print out (write) those Chinese words from the R code. Here is the example of the code on the console:
fi <- 'c:\\R_Routines\\Ch.txt'
g <- readLines(con=fi, encoding='UTF-8')
g
# [1] "发动机测谎报告"
fo <- 'c:\\R_Routines\\Ch1.txt'
fa <- file(fo, encoding='UTF-8')
writeLines(g, con= fa)
fi <- 'c:\\R_Routines\\Ch1.txt'
g <- readLines(con=fi, encoding='UTF-8')
g
# [1] "<U+53D1><U+52A8><U+673A><U+6D4B><U+8C0E><U+62A5><U+544A>"
How to output (write on disk) a text file with the Chinese words I read from the file?
Upvotes: 3
Views: 1425
Reputation: 263301
Not really an answer, but my searching has found several postings suggesting you are not the first person to have trouble with encodings in RStudio. The defaults are accessible with:
That was a screen shot froma Windows user. And this is where I found possible location for default settings on a Mac:
You may also need to use Sys.setlocale
, but I'd try changing hte RStudio defaults first since it seems as though you were able to initially print the characters. I'm unable to confirm your problem on a Mac running the R.app GUI or running RStudio on the Mac:
> fi <- '~/test.txt'
> g <- readLines(con=fi, encoding='UTF-8')
> g
[1] "发动机测谎报告"
The higher values of Unicode are handled with quote-backslash-'u'-numbers-quote. See the help page:
?Quotes
'\u53D1'
[1] "发"
But trying to replace "<U+"
with "\U"
will fail because the R parser will not accept a naked "\U" without succeeding hexadecimal numbers.
Upvotes: 1