Debian Codegraph @

R/utils.R

raw
1 #' @importFrom utils head
2 df_head <- function(x, n) {
3 if (!is.data.frame(x)) {
4 as.data.frame(head(x, n))
5 } else {
6 vec_head(as.data.frame(x), n)
7 }
8 }
9
10 vec_head <- function(x, n) {
11 n <- min(n, vec_size(x))
12 vec_slice(x, seq_len(n))
13 }
14
15 cat_line <- function(...) {
16 cat(..., "\n", sep = "")
17 }
18
19 #' @importFrom cli ansi_substr
20 str_trunc <- function(x, width, shorten = NULL) {
21 if (all(is.infinite(width))) {
22 return(x)
23 }
24
25 str_width <- get_extent(x)
26
27 too_wide <- which(!is.na(x) & str_width > width)
28 if (any(too_wide)) {
29 x[too_wide] <- str_add_ellipsis(x[too_wide], str_width[too_wide], width, shorten)
30 }
31
32 x
33 }
34
35 str_add_ellipsis <- function(x, str_width, width, shorten) {
36 if (length(x) == 0L) {
37 return(x)
38 }
39 shorten <- shorten %||% "back"
40
41 thing <- switch(shorten,
42 back = {
43 abbr <- ansi_substr(x, 1, width - 1)
44 paste0(abbr, get_ellipsis())
45 },
46 untick = str_add_ellipsis_untick(x, str_width, width),
47 untick_footnote = str_add_ellipsis_untick(x, str_width, width, footnote = TRUE),
48 front = {
49 abbr <- ansi_substr(x, str_width - width + 2, str_width)
50 paste0(get_ellipsis(), abbr)
51 },
52 mid = {
53 front_width <- ceiling(width / 2) - 1
54 back_width <- width - front_width - 1
55 abbr_front <- ansi_substr(x, 1, front_width)
56 abbr_back <- ansi_substr(x, str_width - back_width + 1, str_width)
57 paste0(abbr_front, get_ellipsis(), abbr_back)
58 },
59 abbreviate = {
60 abbreviate(x, minlength = width, strict = TRUE)
61 }
62 )
63 thing
64 }
65
66 str_add_ellipsis_untick <- function(x, str_width, width, footnote = FALSE) {
67 stopifnot(length(x) == 1)
68 stopifnot(str_width > width)
69
70 if (footnote) {
71 width <- width - 1L
72 }
73
74 # Removing ticks due to https://github.com/tidyverse/tibble/issues/838
75 x_unticked <- gsub("`", "", x, fixed = TRUE)
76 if (x_unticked != x) {
77 x <- x_unticked
78 str_width <- get_extent(x)
79 }
80
81 # Add ellipsis even if short enough after removal of ticks
82 # TODO use ansi_strim()
83 abbr <- ansi_substr(x, 1, width - 1L)
84 abbr <- paste0(abbr, get_ellipsis())
85
86 if (footnote) {
87 # Placeholder, regular title can't end with this string,
88 # we can use this to detect a footnote
89 abbr <- paste0(abbr, "\u02df")
90 }
91
92 abbr
93 }
94
95 paste_with_space_if_needed <- function(x, y) {
96 if (y == "") {
97 x
98 } else {
99 paste(x, y)
100 }
101 }
102
103 check_sigfig <- function(x) {
104 stopifnot(is.numeric(x), length(x) == 1)
105 x <- as.integer(x)
106 if (x < 1L) {
107 cli::cli_abort("Must show at least one significant figure")
108 }
109 if (x > 15L) {
110 cli::cli_abort("{.arg sigfig} cannot be larger than {.val {15}}.")
111 }
112
113 x
114 }
115
116 slice <- function(df, index) {
117 df[index, , drop = FALSE]
118 }
119
120 bind_rows <- function(x) {
121 if (length(x) == 0) {
122 return(data.frame())
123 }
124 eval_tidy(quo(rbind(!!!x)))
125 }
126
127 get_ellipsis <- function() {
128 symbol$continue
129 }
130
131 is_latex_output <- function() {
132 if (!("knitr" %in% loadedNamespaces())) {
133 return(FALSE)
134 }
135 get("is_latex_output", asNamespace("knitr"))()
136 }
137
138 remove_as_is_class <- function(x) {
139 if (all(class(x) == "AsIs")) {
140 return(unclass(x))
141 }
142 class(x) <- setdiff(class(x), "AsIs")
143 x
144 }
145
146 v <- function(x) {
147 expr <- rlang::expr_deparse(substitute(x), width = Inf)
148 paste0(expr, " = ", rlang::expr_deparse(x, width = 80)[[1]])
149 }
150
151 # Needed for R 3.4 and earlier
152 safe_is_na <- function(x) {
153 if (is.null(x)) {
154 return(logical())
155 }
156 is.na(x)
157 }
158
159 safe_any_na <- function(x) {
160 anyNA(x)
161 }
162
163 vec_lead <- function(x, default = NULL) {
164 n <- vec_size(x)
165 if (n == 0) {
166 return(x)
167 }
168 if (is.null(default)) {
169 default <- vec_slice(x, NA_integer_)
170 }
171 vec_c(vec_slice(x, -1), default)
172 }
173
174 vec_lag <- function(x, default = vec_slice(x, NA_integer_)) {
175 n <- vec_size(x)
176 if (n == 0) {
177 return(x)
178 }
179 if (is.null(default)) {
180 default <- vec_slice(x, NA_integer_)
181 }
182 vec_c(default, vec_slice(x, -n))
183 }
184
185 super <- c(
186 "\u00b9", "\u00b2", "\u00b3", "\u2074",
187 "\u2075", "\u2076", "\u2077", "\u2078", "\u2079",
188 "\u02df"
189 )
190
191 superdigit <- function(x) {
192 if (cli::is_utf8_output()) {
193 super[[min(x, 10)]]
194 } else {
195 if (x >= 10) "*" else as.character(x)
196 }
197 }
198
199 superdigit_sep_default <- function() {
200 if (cli::is_utf8_output()) {
201 "\u200b"
202 } else {
203 ": "
204 }
205 }