I'm using rmarkdown and also two useful packages (officedown and flextable) to produce an MS word document. However, when I write in other languages or change the header label of tables (using set_header_labels
) to farsi (using font(cs.family = "Arial Unicode MS", part = "header")
), it does not transform the text to the correct form in MS word (see the picture) although it is correctly shown in the rmarkdown viewer pane. I tested different fonts but the problem persists! I'm wondering if anyone has any solutions to this? Thanks.
Here is a reproducible example:
---
output: officedown::rdocx_document
---
```{r message=F, warning=F, echo=F}
library(sysfonts)
library(flextable)
library(officedown)
library(dplyr)
library(extrafont)
iris %>% head() %>% flextable() %>% set_header_labels(Sepal.Length="طول کاسبرگ") %>%
font(j = 1, part = "header", fontname = "B Nazanin")
#you can choose other fonts like Arial Unicode MS too but it doesn't make any difference.
```
rmarkdown::render(input = paste0(getwd(),"/","ex.Rmd"), encoding="UTF-8")
The new version of R is more compatible with UTF-8.
https://cran.r-project.org/bin/windows/base/NEWS.R-4.2.1.html
Please update your R, OS, and packages then try again.
It worked for me.
> sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=C
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 digest_0.6.29 R6_2.5.1 officedown_0.2.4
[5] evaluate_0.15 zip_2.2.0 gdtools_0.2.4 cachem_1.0.6
[9] rlang_1.0.3 cli_3.3.0 uuid_1.1-0 xml2_1.3.3
[13] rmarkdown_2.14 tools_4.2.1 officer_0.4.3 xfun_0.31
[17] yaml_2.3.5 fastmap_1.1.0 compiler_4.2.1 systemfonts_1.0.4
[21] rvg_0.2.5 memoise_2.0.1 htmltools_0.5.2 knitr_1.39
>