Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it available for Chinese Characters? #89

Closed
wangay21 opened this issue Feb 16, 2022 · 2 comments
Closed

Is it available for Chinese Characters? #89

wangay21 opened this issue Feb 16, 2022 · 2 comments

Comments

@wangay21
Copy link

I saved Rscript with UTF-8 to allow Chinese Character then it will report as below
image
It seems that I cannot input footnotes in Chinese, so I removed these footnotes, then it could output an RTF file but with all Chinese strings are unreadable.

@elong0527
Copy link
Collaborator

elong0527 commented Feb 16, 2022

Please try the code below for an ad-hoc solution. I will need to think a robust way to enable this feature in r2rtf.

If you still have issue, could you provide additional example using reprex? https://reprex.tidyverse.org/

the logic of convert_utf8 is from https://stackoverflow.com/questions/66275158/specify-utf-8-character-encoding-in-rtf-the-text-in-utf-8-format-is-correctly

#' Convert UTF text to proper RTF encoding 
#' 
#' @param x a string 
convert_utf8 <- function(x){

  stopifnot(length(x) == 1)
  
  x_int <- utf8ToInt(x)
  
  x_char <- ifelse(x_int < 32768, 
                   paste0("\\uc1\\u", x_int, "*"), 
                   paste0("\\uc1\\u", x_int - 65536, "*"))
  
  paste(x_char, collapse = "")
  
}

library(r2rtf)
library(dplyr)
head(iris) %>% 
  rtf_title(convert_utf8("标题: abc")) %>% 
  rtf_body() %>% 
  rtf_footnote(convert_utf8("脚注")) %>%
  rtf_encode() %>% 
  write_rtf("tmp.rtf")

@elong0527
Copy link
Collaborator

Closing the issue.

With a recent fix in #93, we can use code below for the table.

library(r2rtf)
library(dplyr)
head(iris) %>% 
  rtf_title(r2rtf:::utf8Tortf("标题: abc")) %>% 
  rtf_body() %>% 
  rtf_footnote(r2rtf:::utf8Tortf("中文脚注: abc")) %>%
  rtf_encode() %>% 
  write_rtf("tmp.rtf")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants