R/utils.R

Defines functions fix_windows_histograms is_windows make_utf8 fix_unicode

Documented in fix_windows_histograms

# Adapted from
# https://stackoverflow.com/questions/28248457
fix_unicode <- function(char) {
  m <- gregexpr("<U\\+[0-9a-fA-F]{4}>", char)
  codes <- regmatches(char, m)
  chars <- lapply(codes, make_utf8)
  regmatches(char, m) <- chars
  enc2utf8(char)
}

make_utf8 <- function(x) {
  if (length(x) < 1) {
    x
  } else {
    digits <- substring(x, 4, 7)
    integers <- strtoi(sprintf("0x%s", digits))
    purrr::map_chr(integers, intToUtf8)
  }
}

is_windows <- function() {
  tolower(Sys.info()[["sysname"]]) == "windows"
}

#' Fix unicode histograms on Windows
#'
#' This functions changes your session's locale to address issues with printing
#' histograms on Windows.
#'
#' There are known issues with printing the spark-histogram characters when
#' printing a data frame, appearing like this: "<U+2582><U+2585><U+2587>".
#' This longstanding problem originates in the low-level code for printing
#' dataframes.
#'
#' @seealso [skim_without_charts()]
#' @export
fix_windows_histograms <- function() {
  message(
    "This function will change your system locale. It may have other ",
    "unintended effects."
  )
  response <- readline("Continue? (Y/n)")
  if (tolower(response) != "n") {
    Sys.setlocale("LC_CTYPE", "Chinese")
  } else {
    message("Locale was not changed.")
  }
  invisible(NULL)
}

Try the skimr package in your browser

Any scripts or data that you put into this service are public.

skimr documentation built on Dec. 28, 2022, 2:45 a.m.