File: utils.R

package info (click to toggle)
r-cran-skimr 2.1.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,188 kB
  • sloc: sh: 13; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,314 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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)
}