How to display a message/warning/error with Unicod

2020-04-02 07:45发布

问题:

I have a message (or warning or error) containing Unicode characters. (The string has UTF-8 encoding.)

x <- "\u20AC \ub124" # a euro symbol, and Hangul 'ne'
## [1] "€ 네"
Encoding(x)
## [1] "UTF-8"

Under Linux, this prints OK in a message if the locale is UTF-8 (l10n_info()$`UTF-8` returns TRUE).

I can force this, by doing, e.g.,

devtools::with_locale(
  c(LC_CTYPE = "en_US.utf8"),
  message(x)  
)
## € 네

Under Windows there are no UTF-8 locales, so I can't find an equivalent way to enforce correct printing. For example, with a US locale, the Hangul character doesn't display properly.

devtools::with_locale(
  c(LC_CTYPE = "English_United States"),
  message(x)  
)
## € <U+B124>

There's a related problem with Unicode characters not displaying properly when printing data frames under Windows. The advice there was to set the locale to Chinese/Japanese/Korean. This does not work here.

devtools::with_locale(
  c(LC_CTYPE = "Korean_Korea"),
  message(x)  
)
## ¢æ ³×   # equivalent to iconv(x, "UTF-8", "EUC-KR")

How can I get UTF-8 messages, warnings and errors to display correctly under Windows?

回答1:

I noticed that the help for the function Sys.setlocale() in R says this: "LC_MESSAGES" will be "C" on systems that do not support message translation, and is not supported on Windows.

To me this sounds like modifying character representation for R messages/errors can't be done on any Windows version...



标签: r unicode utf-8