tomcai tomcai - 2 months ago 8
R Question

when use rstudio make r package, the package cant output chinese words,why?

I try to use rstudio to make r package. but the package cant print the right words.

below is the code in package:

hello3 <- function(){
print("测试")
}


but when use the package ,the output is :

> library(hr)
> hello3()
[1] "娴嬭瘯"


It's evident that the output text into other encodings.

What reason is this?

How to solve this problem?

Answer

Using multi-byte characters in your source code makes a lot of headache. I would use unicode expression as below.

hello3 <- function() 
{
  print("\u6d4b\u8bd5")
}

Then,

hello3()
[1] "测试"

If you do not know what is the right expression for your words, try:

sprintf("%x", utf8ToInt("测试"))
[1] "6d4b" "8bd5"
Comments