gagolews / stringi

Fast and portable character string processing in R (with the Unicode ICU)
https://stringi.gagolewski.com/
Other
304 stars 44 forks source link

Stri_write_lines to a csv file #390

Closed lancearius163 closed 3 years ago

lancearius163 commented 4 years ago

The Stri_write_lines function is very useful for me to export files in Chinese from R to windows via txt files. However I am going to write a csv file of tables with Chinese, is there any similar functions that I can use? Thank you!

gagolews commented 4 years ago

stri_write_lines is implemented as follows:

function (str, fname, encoding = "UTF-8", sep = ifelse(.Platform$OS.type == 
    "windows", "\r\n", "\n")) 
{
    str <- stri_join(str, sep, collapse = "")
    str <- stri_encode(str, "", encoding, to_raw = TRUE)[[1]]
    writeBin(str, fname, useBytes = TRUE)
    invisible(NULL)
}

I think the easiest approach would be to open a string connection (see textConnection), call write.csv() on it, apply a conversion with stri_encode and call writeBin

Do you have a concrete (reproducible) example where setting the fileEncoding argument of read.csv or read.table does not give you the correct output?

gagolews commented 3 years ago

(closing due to inactivity)