runicodeutf-8write.table

Format problem of the write.table in R, utf-8 original encode


There is a problem with the write.table. chr2 is correctly shown in the console but fail in the saved txt file.

> chr1 = chr2 = rep(NA, 8)
> for(i in 1:8){
+   chr1[i] = letters[i]
+   chr2[i] = intToUtf8(10240+2^(i-1))
+ } 
> 
> chr1
[1] "a" "b" "c" "d" "e" "f" "g" "h"
> chr2
[1] "⠁" "⠂" "⠄" "⠈" "⠐" "⠠" "⡀" "⢀"
> 

write.table(cbind(chr1, chr2), file = "chr2.txt")

chr.txt

May I know how to get the txt file printing exactly the same way as the console? Thank you!

My OS and R version:

version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          4                           
minor          0.3                         
year           2020                        
month          10                          
day            10                          
svn rev        79318                       
language       R                           
version.string R version 4.0.3 (2020-10-10)
nickname       Bunny-Wunnies Freak Out

---------------- June-06 Update ----------------

In fact, what I really want to do is to save a "txt picture" like:

⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⢀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡟⠉⠻⣿⣦⢀⠘⣿⣿⣿⣿⣿⣿⡏⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣦⡀⠈⠻⣧⢀⠘⢿⡟⢠⣤⣌⡇⢀⣴⣅⣤⣄⠙⡟⢡⣤⣈⡇⢸⠟⢠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⡿⠉⠛⢿⣿⣄⢀⠈⢳⡀⢸⣧⣈⡉⠙⠇⢸⣿⠛⣉⣉⢀⠁⣾⣿⣿⡇⢀⡀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣷⣦⣀⢀⠈⠙⠷⣄⢀⣽⣿⡟⠻⠿⠃⣠⠘⢿⢀⠿⠟⢀⣇⠘⠿⠛⡇⢸⣷⡀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⡿⠛⠻⠿⣿⣦⣄⡀⠈⣿⣿⣿⣿⣶⣶⣾⣿⣷⣾⣷⣶⣾⣶⣿⣷⣶⣾⣷⣾⣿⣷⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⡿⠿⣿⣧⣄⣀⢀⢀⢀⠉⠙⢺⣿⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠁⢀⡇⢀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⡇⢀⣿⡿⠿⠿⠿⠿⠶⠶⠤⣼⣿⢀⣿⡿⠋⠉⠛⡏⠉⣿⡏⠉⡟⠋⠉⠻⡟⠉⠋⠉⠃⢀⠙⡇⢀⣿⠟⠉⠉⠻⠉⠹⣿⠉⢻⡟⠉ ⡇⢀⣿⣇⣀⣀⣀⣀⣀⣀⣀⣿⣿⢀⣿⠁⢠⣶⢀⢰⢀⢹⢀⢸⢀⠐⠓⢀⠁⢀⣰⣶⡇⢀⣶⡇⢀⣿⢀⢰⡆⢀⠃⢀⡇⢀⠈⠇⢀ ⡇⢀⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⢀⣿⡀⠘⠿⢀⢸⡆⢀⢀⣿⢀⠰⡶⠶⡆⢀⣿⣿⡇⢀⣿⡇⢀⢿⢀⠸⠇⢀⣼⢀⢀⣸⢀⢀⣸ ⣇⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣿⣷⣄⣀⣠⣿⣿⣀⣸⣿⣦⣀⣀⣠⣧⣀⣿⣿⣇⣀⣿⣷⣄⣀⣦⣀⣀⣴⣿⣆⣀⣿⣇⣀⣿

I have uploaded my code and the example picture to GitHub:

https://github.com/CarltonChen/TxtDotPic

Please have a look.


Solution

  • Unfortunately, coercing the two vectors to a data.frame to write it as a table already converts the characters to unicode references. You can see that when you use data.frame(chr1,chr2).

    I frankly do not know how to write unicode symbols to tables, but if you are able to compose lines of a file (concatenate the values with '\t'), you can write them using writeLines() to a file with native encoding:

    f = file("specialchar.txt",open="w",encoding="native.enc")
    writeLines(paste(chr1,chr2,sep='\t'),con=f, useBytes=T)
    close(f)
    

    When I do this, I get the following result:

    enter image description here