File: helpers-misc.R

package info (click to toggle)
r-cran-tidyselect 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 616 kB
  • sloc: sh: 13; makefile: 2
file content (79 lines) | stat: -rw-r--r-- 2,017 bytes parent folder | download | duplicates (2)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#' Select all variables or the last variable
#'
#' @description
#'
#' These functions are [selection helpers][language].
#'
#' * [everything()] selects all variable. It is also useful in
#'   combination with other tidyselect operators.
#'
#' * [last_col()] selects the last variable.
#'
#' @inheritParams starts_with
#'
#' @section Examples:
#'
#' ```{r, child = "man/rmd/setup.Rmd"}
#' ```
#'
#' Selection helpers can be used in functions like `dplyr::select()`
#' or `tidyr::pivot_longer()`. Let's first attach the tidyverse:
#'
#' ```{r, comment = "#>", collapse = TRUE}
#' library(tidyverse)
#'
#' # For better printing
#' iris <- as_tibble(iris)
#' mtcars <- as_tibble(mtcars)
#' ```
#'
#' Use `everything()` to select all variables:
#'
#' ```{r, comment = "#>", collapse = TRUE}
#' iris %>% select(everything())
#'
#' mtcars %>% pivot_longer(everything())
#' ```
#'
#' Use `last_col()` to select the last variable:
#'
#' ```{r, comment = "#>", collapse = TRUE}
#' iris %>% select(last_col())
#'
#' mtcars %>% pivot_longer(last_col())
#' ```
#'
#' Supply an offset `n` to select a variable located `n` positions
#' from the end:
#'
#' ```{r, comment = "#>", collapse = TRUE}
#' mtcars %>% select(1:last_col(5))
#' ```
#'
#' @seealso `r rd_helpers_seealso()`
#' @export
everything <- function(vars = NULL) {
  vars <- vars %||% peek_vars(fn = "everything")
  seq_along(vars)
}

#' @rdname everything
#' @export
#' @param offset Set it to `n` to select the nth var from the end.
last_col <- function(offset = 0L, vars = NULL) {
  if (!is_integerish(offset, n = 1)) {
    not <- obj_type_friendly(offset)
    cli::cli_abort("{.arg offset} must be a single integer, not {not}.")
  }

  vars <- vars %||% peek_vars(fn = "last_col")
  n <- length(vars)

  if (offset && n <= offset) {
    cli::cli_abort("{.arg offset} ({offset}) must be smaller than the number of columns ({n}).")
  } else if (n == 0) {
    cli::cli_abort("Can't select last column when input is empty.")
  } else {
    n - as.integer(offset)
  }
}