简体   繁体   中英

Exporting special characters from R to excel/csv

I have a vector in R that contains special characters, eg x = "Köln" .

I have trouble exporting that back to either excel or csv format (preferably in Excel).

Here is what I tried:

`#Exporting to excel
library(excel.link)
myExcelProcess = COMCreate("Excel.Application")
xl.workbook.add()
excelRange = myExcelProcess[["ActiveSheet"]]$Range("A1:A1")
xl.write(x, excelRange, row.names = FALSE) # this creates an excel file with "ruined" characters (Köln)`

#Exporting to csv write.csv(x = routesCities, file = "out.csv", fileEncoding = "UTF-8") # This creates a csv containing "Koln" - the Umlaut has disappeared.

Any help would be greatly appreciated.

EDIT (Regarding the flagging as possible duplicate): imho the question is rather different than the proposed duplicate, since it refers to encoding issues during data transfer between R and Excel, not failing to run a write.xlsx() function. Furthermore, my attempts do not result in an error (there is no error message), but the results are not the desired ones. It might be the case that the same answer (using a different library) could apply here, too, but the question itself is quite different.

The readr package that is part of the tidyverse has a function write_excel_csv that exports data frames to csv in UTF-8 encoding in such a way that opening them directly in excel preserves special characters. While I haven't tried exhaustively, I had a similar issue with exporting data with the "greater than or equal to" symbol ≥, represented in string variables as the string \≥. It came out perfectly using this function, even when my previous attempts at using write.csv , even with the option "fileEncoding="UTF-8", had still left strange symbols in the file when opened in excel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM