title: Sweave doesn't seem to get .Rnw file encoding right
body: <p>This question arose out of the following question on tex.sx: <a href="https://tex.stackexchange.com/q/28710/2693">Sweave generating invalid LaTeX</a>. The problem seems to be that <code>Sweave</code> is not recognizing the encoding of the file, despite the locale being set to UTF-8, and the <code>.Rnw</code> file being saved as UTF-8.  The end result is that any <code>.Rnw</code> file that contains non-ASCII characters ends up producing NA in the resultant <code>.tex</code> file. As you can read in the comments to that question, another user doesn't show the problem, with what is apparently an identical setup. (R 2.13.1 on a Mac) Here's a minimal document that fails.</p>
  
  <p><strong>Update</strong> </p>
  
  <p>Based on Aaron's suggestions, I've added <code>sessionInfo</code> to the <code>.Rnw</code> file, and now the real problem reveals itself. When <code>Sweave</code> processes the file, it seems to change the locale.</p>
  
  <p><strong><code>.Rnw</code> file</strong></p>
  
  <pre><code>\documentclass{article}
  \usepackage[utf8]{inputenc}
  \begin{document}
  Some non-ascii text: éüáî
  &lt;&lt;&gt;&gt;=
  sessionInfo()
  @ 
  \end{document}
  </code></pre>
  
  <p>Running this through <code>Sweave</code>, produces the following <code>.tex</code> file. The line containing the non-ASCII characters has been converted into <code>NA</code> by <code>Sweave</code>. It seems also that the locale has been changed:</p>
  
  <p><strong>Resultant <code>.tex</code> file</strong></p>
  
  <pre><code>\documentclass{article}
  \usepackage[utf8]{inputenc}
  \usepackage{Sweave}
  \begin{document}
  NA
  \begin{Schunk}
  \begin{Sinput}
  &gt; sessionInfo()
  \end{Sinput}
  \begin{Soutput}
  R version 2.13.1 (2011-07-08)
  Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
  
  locale:
  [1] C
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  loaded via a namespace (and not attached):
  [1] tools_2.13.1
  \end{Soutput}
  \end{Schunk}
  \end{document}
  </code></pre>
  
  <p><code>sessionInfo()</code> from within <code>R.app</code> returns:</p>
  
  <pre><code>&gt; sessionInfo()
  R version 2.13.1 (2011-07-08)
  Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
  
  locale:
  [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
  </code></pre>
  
  <p><strong>Update (Response to Aaron)</strong></p>
  
  <pre><code>&gt; text &lt;- readLines("sweave-enc-test.Rnw", warn = FALSE)
  &gt; enc &lt;- tools:::.getVignetteEncoding(text, convert = TRUE)
  &gt; 
  &gt; text
  [1] "\\documentclass{article}"     "\\usepackage[utf8]{inputenc}" "\\begin{document}"           
  [4] "Some non-ascii text: éüáî"    "\\end{document}"             
  &gt; enc
  [1] "UTF-8"
  &gt; iconv(text, enc, "")
  [1] "\\documentclass{article}"     "\\usepackage[utf8]{inputenc}" "\\begin{document}"           
  [4] "Some non-ascii text: éüáî"    "\\end{document}"      
  </code></pre>
  
  <p>(This is the output from within the R console in <code>R.app</code>.)</p>