3

I am having problems to write a csv file with Spanish accents, using R 3.1.2 and Mac OS X 10.6.

I cannot write words with accents into text file.

When I do:

con <- file("y.csv",encoding="UTF-8")
write.csv("Ú",con)

I get y.csv file which has the following content:

"","x" "1","√ö"

Ie, "√ö" instead of "Ú".

When using write.table the outcome is equivalent.

Encoding("Ú") is "UTF-8"

If I do write.xlsx("Ú","y.xlsx") I get y.xlsx file which successfully shows Ú.

I have also tried to convert to other encodings using iconv() with no success.

I have set default encoding "UTF-8" in RStudio and on TextEdit. When using only R (not RStudio) the problem is the same. In RStudio the special characters appear correctly (in files), and also in the console in R.

Sys.getlocale()gives

"es_ES.UTF-8/es_ES.UTF-8/es_ES.UTF-8/C/es_ES.UTF-8/es_ES.UTF-8"

In Mac OS X Terminal

file -I y.csv

gives

y.csv: text/plain; charset=utf-8

I don't see where the problem is. Any help, please?

fcanomar
  • 31
  • 1
  • 3
  • possible duplicate of [Export UTF-8 BOM to .csv in R](http://stackoverflow.com/questions/7402307/export-utf-8-bom-to-csv-in-r) – beresfordt Mar 19 '15 at 21:40

3 Answers3

0

Just came across this other question that seems like a near duplicate to this one :

Export UTF-8 BOM to .csv in R

Community
  • 1
  • 1
0

The problem was not one of codification in R, but from TextEdit, which did not show the right characters, although I had selected UTF-8 codification in preferences. Got solved using a different editor. I was using Mac OS X 10.6.8 and TextEdit 1.6.

fcanomar
  • 31
  • 1
  • 3
0

Maybe you cán write words with accents, but Excel expects a different encoding. Try writing your csv with for example write_csv(), and open the csv with a workaround:

  • open Excel
  • then choose tab Data | Get External Data | From Text
  • choose your file
  • and in step 1 of the text import wizard, choose file origin 65001: Unicode(UTF8).

See also http://www.openforis.org/support/questions/279/wrong-characters-display-when-exporting-files-to-csv-from-collect

MartineJ
  • 591
  • 5
  • 16