2

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")

enter image description here

tarleb
  • 19,863
  • 4
  • 51
  • 80
M. white
  • 80
  • 7
  • Could you edit your question and add a [minimal reproducible example](https://en.m.wikipedia.org/wiki/Minimal_reproducible_example)? That would make it easier for us to help. – tarleb Jul 09 '22 at 15:16
  • Do you have the languages configured in MS Office? [Like this](https://support.microsoft.com/en-us/office/language-accessory-pack-for-office-82ee1236-0f9a-45ee-9c72-05b026ee809f#ID0EBBF=Office_2021). – Kat Jul 09 '22 at 17:10
  • @Kat yes. I have Persian enabled. – M. white Jul 10 '22 at 07:56
  • @tarleb I accordingly added an example. – M. white Jul 10 '22 at 07:57
  • this sounds like a regression in 0.7.2 that has been solved few days ago: see https://github.com/davidgohel/flextable/issues/419. Can you install the github version and see if it solved your issue? – David Gohel Jul 10 '22 at 09:56
  • @DavidGohel I installed it from Github and it didn't work :( I also tried adding "lang: ar" in YAML and it successfully works in only changing the body numbers and doesn't solve the header. – M. white Jul 10 '22 at 11:06
  • @DavidGohel do you have any solutions in mind? – M. white Jul 11 '22 at 17:36
  • yes, I think you are not working in UTF8 and that's probably the root of the issue. You can check with `readr::guess_encoding()` – David Gohel Jul 12 '22 at 07:24
  • @DavidGohel As you see I haven't used any external files in this example. My R script and Rmd files are both saved with UTF-8 and the number transformations are excellent. Have you ever checked the compatibility of the package with these languages' text (and not just numbers)? As stated here: https://davidgohel.github.io/flextable/reference/font.html. If so I would think of installing a newer MS office version. Thanks again for this valuable package. – M. white Jul 12 '22 at 17:31
  • yes Mr White, I did! and your example works on UTF-8 systems. I faced this issue with users several times and it's always UTF-8 pbs. – David Gohel Jul 12 '22 at 22:06

1 Answers1

2

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.

enter image description here

> 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       
> 
Ali
  • 46
  • 1
  • 6
  • Thanks, @Ali agha. My OS version was a bit old and apparently UTF-8 support on Windows is widely enhanced after 2019. The issue was resolved after installing a suitable version. – M. white Jul 18 '22 at 17:01