[R] UTF-8 to the console

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Jun 23 13:36:16 CEST 2022

On Thu, 23 Jun 2022 12:26:23 +0200
Helmut Schütz <helmut.schuetz using bebac.at> wrote:

> txt <- "x ≥ y, x \u2265 y; a ≈ b, a \u2248 b"
> Encoding(txt) <- "UTF-8"

There shouldn't be a need to change the encoding. If you're creating a
Unicode literal, R should already choose UTF-8 for the resulting
string. Either way, R automatically converts the strings from their
source encoding on output.

Moreover, `Encoding<-` doesn't perform any conversion, it only changes
the declared encoding on the string, affecting the way it may be
encoded or decoded in the future. If Encoding(txt) wasn't already UTF-8,
you would likely be damaging the data:

string <- 'Ы' # is already UTF-8
# No conversion happens, the same bytes re-interpreted differently
Encoding(string) <- 'latin1'
# [1] "Ы"

> R 4.2.0 on Windows 7

On Windows 7, Rterm will stay limited to the OEM encoding, since UCRT
only supports UTF-8 locales on Windows ≥ 10, version 1903. If your OEM
encoding doesn't have the ≥, ≈ characters, printing them to the console
is going to be hard. Not impossible -- e.g. an R extension written in C
could obtain a handle to the current console and use Unicode-aware
Windows API to print these characters -- but just getting it to work
would be hard, and it will be likely unportable.

> and Windows 11.

I think it should be possible. What does system('chcp') say in your
Rterm session?

For console UTF-8 output to work, two things should happen:

1. The console must be using UTF-8, i.e. chcp must say it's using code
page 65001.

2. Rterm must understand that and also use UTF-8 on output.

What does sessionInfo() and l10n_info() say in your Rterm session on
Windows 11? In Rterm source code, I see a check for GetACP() == 65001,
which should have switched the console encoding to UTF-8 automatically.

Perhaps you need to run chcp 65001 before starting Rterm? Maybe you
need to set a checkbox [*] to make the ANSI codepage UTF-8 by default?
I'm not sure any of this is going to work, but it's something to try
before someone more knowledgeable with R on Windows can help you.

Best regards,

[*] https://superuser.com/a/1451686

More information about the R-help mailing list