[R-SIG-Mac] R-SIG-Mac Digest, Vol 94, Issue 2

Denis Chabot chabotd at globetrotter.net
Thu Dec 2 12:49:44 CET 2010


Thanks Prof. Ripley,

Le 2010-12-02 à 06:00, r-sig-mac-request at r-project.org a écrit :

> 
> On Wed, 1 Dec 2010, Denis Chabot wrote:
> 
>> Hi,
>> 
>> Until now, I have always been happy to export dataframes using 
>> write.csv2. The only drawback was than they often include accented 
>> vowels and Excel on Mac seems to ONLY be able to properly understand 
>> MacRoman encoding. So I used OpenOffice instead to open them.
>> 
>> But for the next little while, colleagues using Windows and who do 
>> not have a clue about encoding (and who do not want to know about 
>> encoding) need to use my files. My files will have to be in 
>> ISOLATIN1 encoding.
> 
> Most likely CP1252, which is the encoding used on Windows in W. 
> European languages, not-quite-a-superset of latin1. (This is all in 
> the R manuals, BTW.)
> 
>> It is unfortunate that the write.table family of 
>> functions does not have a FileEncoding option like the read.table 
>> function family.
> 
> But that's just syntactic sugar.
> 
>     file: either a character string naming a file or a connection open
>           for writing.  ?""? indicates output to the console.
> 
> See ?file for how to specify the encoding of a connection.  Very 
> likely you want
> 
> con <- file('filename', open="w", encoding = "latin1")
> 
> (or "macroman": see ?iconvlist to find encoding names on your OS).
> 
> [...]
> 
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595

This works well for me for dataframe "test":

con <- file("test.csv", open="w", encoding = "LATIN1")
write.csv(test, con, row.names=F)
close(con)

I hesitated to use close() because this suggested it would be closed automatically:
"open opens a connection. In general functions using connections will open them if they are not open, but then close them again, so to leave a connection open call open explicitly."

But I quickly discovered I had to close manually.

Denis


More information about the R-SIG-Mac mailing list