There's no completely simple way to do this within RStudio. The reason for this is that the compiledPdf
command that RStudio provides uses texi2dvi
to compile the LaTeX file generated by Sweave
, and texi2dvi
knows nothing about xelatex
, which is what you should be using for your Hebrew documents. So the solution is to run Sweave
manually from within RStudio, and then run xelatex
from the console. To get you started, here's a test document that I had lying around for a separate question here on the site that I have adapted for use with Hebrew.
Update The development version of RStudio (versions > 0.96.48) now supports XeLaTeX.
Rnw file (assume name is sweave-test.Rnw
)
% This document must be compiled with Sweave and xelatex (not pdflatex)
\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}
\usepackage[noae]{Sweave} % you must load Sweave with the `noae` option
% load polyglossia late in the package load order, since the bidi package (which is
% loaded when an RTL language is set) redefines lots of package code.
\usepackage{polyglossia}
\setmainlanguage{hebrew}
\setotherlanguage{english}
\setmainfont{David CLM}
% for R work with Hebrew, it's important to have a mono font that supports
% the Hebrew script. Miriam Mono is one that I know of
\setmonofont[Script=Hebrew]{Miriam Mono CLM}
\title{\textenglish{Side-by-side xtables}}
\author{}
\date{}
\begin{document}
\maketitle
ראשית קצת קוד R כדי ליצור כמה נתונים.
<<>>=
myData <- matrix(c(19,89,23,23,74,44,16,39,67),ncol=3,byrow=TRUE)
colnames(myData) <- c("A","B","C")
rownames(myData) <- c("1","2","3")
myData2 <- myData * 2
@
כעת אנו שמים את הנתונים בשני צדדי על ידי צד טבלאות:
\begin{table}[htb]
\begin{minipage}{.45\textwidth}
\centering
\captionof{table}{\textenglish{First Table}}
<<echo=FALSE,results=tex>>=
library("xtable")
print(xtable(myData),
floating=FALSE,
hline.after=NULL,
add.to.row=list(pos=list(-1,0, nrow(myData)),
command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@
\end{minipage}
\begin{minipage}{.45\textwidth}
\centering
\captionof{table}{\textenglish{Second Table}}
<<echo=FALSE,results=tex>>=
print(xtable(myData2),
floating=FALSE,
hline.after=NULL,
add.to.row=list(pos=list(-1,0, nrow(myData2)),
command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@
\end{minipage}
\end{table}
\end{document}
Open this document within RStudio, and then run Sweave on it:
Sweave("sweave-test.Rnw")
This should then exit with the following message:
Writing to file sweave-test.tex
Processing code chunks with options ...
1 : echo term verbatim
2 : term tex
3 : term tex
You can now run (pdf)latex on 'sweave-test.tex'
Now you need to run xelatex
(not pdflatex
) on the resultant .tex
file. Assuming your system is set up to run correctly from the command line, this is probably easiest to do by opening a Shell window from within RStudio, and then running the command from there.
xelatex sweave-test.tex
Then you will need to open the resulting .pdf
file with your favourite viewer. Since the latest stable release of RStudio isn't very customizable, I don't think there's a simple way around this. I don't use RStudio myself, though; on a Mac I can run Sweave files directly from within my LaTeX IDE (TeXShop).
Using XeLaTeX with later versions of RStudio
Currently version 0.96.48 of RStudio now has preferences to use XeLaTeX for Sweave compilation. In the preferences, choose XeLaTeX for compilation, and make sure the "Use texi2dvi to resolve cross references` is unchecked. Here's an image of the preference panel.
Output
Here is the output. Since I don't know Hebrew, I've just used Google translate to translate the little bit of text in the document. I'm imagine it's quite amusing.
Best Answer
Use instead
results='asis'
:If you switch from Sweave to knitr, you probably need to read this page: http://yihui.name/knitr/demo/sweave/