[R] writing character to a file in UTF-8

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 23 13:54:34 CET 2006


On Thu, 23 Nov 2006, ronggui wrote:

> I would like to now if there a universal way to writing a character
> vector to a file in UTF-8 encoding? By " universal", I mean a way
> which works under Linux, Windows and Mac.

You need to know what encoding the character vector is in.  If you know it 
to be in the charset of the current locale, something like

z <- file("outfile", "w", encoding="UTF-8")
write(char_vector, file=z)
close(z)

If not, you could use iconv() to convert it to UTF-8 and then use write() 
without specifying the encoding.

-- 
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



More information about the R-help mailing list