amin roshani amin roshani - 3 months ago 16
R Question

farsi character in knitr

I am using knitr 1.13 in R 3.3.1 and within RStudio and having problems with knitr recognizing Farsi characters. I am saving below code in

test.Rnw
file and then run
knit("test.Rnw")
but Farsi characters change to
?????
in output.

\documentclass{article}
\usepackage{xepersian}%package for type Farsi in LaTex
\begin{document}
این یک متن آزمایشی است
<<model>>=
fit <- lm(dist ~ speed, data = cars)
@
برای آزمایش داریم:
\Sexpr{coef(fit)[2]}
\end{document}

Answer
  1. Ensure that the file is saved in UTF-8 encoding
  2. On most operating systems, that’s enough, since UTF-8 is the system’s default encoding anyway. Especially on Windows, you additionally need to specify the encoding when knitting, e.g.:

    knit('filename.rnw', encoding = 'UTF-8')
    
Comments