output_utf8 {utf8} | R Documentation |
Test whether the output connection has ANSI style escape support or UTF-8 support.
output_ansi() output_utf8()
output_ansi
tests whether the output connection supports ANSI
style escapes. This is TRUE
if the connection is a terminal
and not the Windows GUI. Otherwise, it is true if running in
RStudio 1.1 or later with ANSI escapes enabled, provided stdout()
has not been redirected to another connection by sink()
.
output_utf8
tests whether the output connection supports UTF-8. For
most platforms l10n_info()$`UTF-8`
gives this information, but this
does not give an accurate result for Windows GUIs. To work around this,
we proceed as follows:
if the character locale (LC_CTYPE
) is "C"
, then
the result is FALSE
;
otherwise, if l10n_info()$`UTF-8`
is TRUE
,
then the result is TRUE
;
if running on Windows in RGui or RStudio, then the result
is TRUE
unless stdout()
has been redirected;
in all other cases the result is FALSE
.
A logical scalar indicating whether the output connection supports the given capability.
.Platform
, isatty
, l10n_info
,
Sys.getlocale
# test whether ANSI style escapes or UTF-8 output are supported cat("ANSI:", output_ansi(), "\n") cat("UTF8:", output_utf8(), "\n") # switch to C locale Sys.setlocale("LC_CTYPE", "C") cat("ANSI:", output_ansi(), "\n") cat("UTF8:", output_utf8(), "\n") # switch to native locale Sys.setlocale("LC_CTYPE", "") tmp <- tempfile() sink(tmp) # redirect output to a file cat("ANSI:", output_ansi(), "\n") cat("UTF8:", output_utf8(), "\n") sink() # restore stdout # inspect the output readLines(tmp)