File: format.R

package info (click to toggle)
r-cran-recipes 0.1.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,496 kB
  • sloc: sh: 37; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 1,043 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
#' Helpers for printing step functions
#'
#' @param x A vector of objects.
#' @param sep A character string for separating values.
#' @param width An integer for when to split the output over lines.
#' @return A character string
#' @keywords internal
#' @export
format_ch_vec <-
  function(x,
           sep = ", ",
           width = options()$width - 9) {
    widths <- nchar(x)
    sep_wd <- nchar(sep)
    adj_wd <- widths + sep_wd
    if (sum(adj_wd) >= width) {
      keepers <- max(which(cumsum(adj_wd) < width)) - 1
      if (length(keepers) == 0 || keepers < 1) {
        x <- paste(length(x), "items")
      } else {
        x <- c(x[1:keepers], "...")
      }
    }
    paste0(x, collapse = sep)
  }



#' @keywords internal
#' @rdname format_ch_vec
#' @export
format_selectors <- function(x, width = options()$width - 9) {
  ## convert to character without the leading ~
  x_items <- lapply(x, function(x) {
    expr_deparse(quo_get_expr(x))
  })

  x_items <- unlist(x_items)
  format_ch_vec(x_items, width = width, sep = ", ")
}