[R] Input encoding problem when using sweave with xetex

Duncan Murdoch murdoch.duncan at gmail.com
Wed May 12 18:45:51 CEST 2010

On 12/05/2010 11:36 AM, Erich Studerus wrote:
> Putting \usepackage[cp1252]{inputenc} into my preamble is not an option,
> because XeTeX unlike LaTeX needs UTF-8 has input encoding. My goal is also
> to have a LyX document that can be compiled both on Mac and Windows.
> I usually compile my Lyx-Sweave documents by one click of a button from
> within Lyx. R code chunks are therefore executed by calling R from the
> command line. If anybody knows how to run R with options(encoding="UTF-8")
> from the command line under windows, that would be helpful.
> The command that calls R during compilation is contained in this file:
> http://cran.r-project.org/contrib/extra/lyx/preferences

You can do it with a little work.  If you look at the 
rhome/bin/Sweave.sh file, you'll see that

 R CMD Sweave file.Rnw

just executes something like

echo "library(\"utils\"); Sweave(\"file.Rnw\")" | R --no-restore --slave

What you want is to execute

echo "library(\"utils\"); options(encoding="UTF-8"); 
Sweave(\"file.Rnw\")" | R --no-restore --slave

You could edit the rhome/bin/Sweave.sh file appropriately if you always 
want Sweave to use UTF-8, or you could edit your Lyx preference file to 
put in a line like this instead of what it had.

Duncan Murdoch

More information about the R-help mailing list