title: Write UTF-8 files from R
body: <p>Whereas R seems to handle Unicode characters well internally, I'm not able to output a data frame in R with such UTF-8 Unicode characters. Is there any way to force this?</p>
  
  <pre><code>data.frame(c("hīersumian","ǣmettigan"))-&gt;test
  write.table(test,"test.txt",row.names=F,col.names=F,quote=F,fileEncoding="UTF-8")
  </code></pre>
  
  <p>The output text file reads:</p>
  
  <p><code>hiersumian &lt;U+01E3&gt;mettigan</code></p>
  
  <p>I am using R version 3.0.2 in a Windows environment (Windows 7).</p>
  
  <p><strong>EDIT</strong></p>
  
  <hr>
  
  <p>It's been suggested in the answers that R is writing the file correctly in UTF-8, and that the problem lies with the software I'm using to view the file. Here's some code where I'm doing everything in R. I'm reading in a text file encoded in UTF-8, and R reads it correctly. Then R writes the file out in UTF-8 and reads it back in again, and now the correct Unicode characters are gone.</p>
  
  <pre><code>read.table("myinputfile.txt",encoding="UTF-8")-&gt;myinputfile
  myinputfile[1,1]
  write.table(myinputfile,"myoutputfile.txt",row.names=F,col.names=F,quote=F,fileEncoding="UTF-8")
  read.table("myoutputfile.txt",encoding="UTF-8")-&gt;myoutputfile
  myoutputfile[1,1]
  </code></pre>
  
  <p>Console output:</p>
  
  <pre><code>&gt; read.table("myinputfile.txt",encoding="UTF-8")-&gt;myinputfile
  &gt; myinputfile[1,1]
  [1] hīersumian
  Levels: hīersumian ǣmettigan
  &gt; write.table(myinputfile,"myoutputfile.txt",row.names=F,col.names=F,quote=F,fileEncoding="UTF-8")
  &gt; read.table("myoutputfile.txt",encoding="UTF-8")-&gt;myoutputfile
  &gt; myoutputfile[1,1]
  [1] &lt;U+FEFF&gt;hiersumian
  Levels: &lt;U+01E3&gt;mettigan &lt;U+FEFF&gt;hiersumian
  &gt; 
  </code></pre>