-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathcontext.R
More file actions
180 lines (162 loc) · 4.39 KB
/
Copy pathcontext.R
File metadata and controls
180 lines (162 loc) · 4.39 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#' Information about the "current" group or variable
#'
#' @description
#' These functions return information about the "current" group or "current"
#' variable, so only work inside specific contexts like [summarise()] and
#' [mutate()].
#'
#' * `n()` gives the current group size.
#' * `cur_group()` gives the group keys, a tibble with one row and one column
#' for each grouping variable.
#' * `cur_group_id()` gives a unique numeric identifier for the current group.
#' * `cur_group_rows()` gives the row indices for the current group.
#' * `cur_column()` gives the name of the current column (in [across()] only).
#'
#' See [group_data()] for equivalent functions that return values for all
#' groups.
#'
#' See [pick()] for a way to select a subset of columns using tidyselect syntax
#' while inside `summarise()` or `mutate()`.
#'
#' @section data.table:
#' If you're familiar with data.table:
#'
#' * `cur_group_id()` <-> `.GRP`
#' * `cur_group()` <-> `.BY`
#' * `cur_group_rows()` <-> `.I`
#'
#' See [pick()] for an equivalent to `.SD`.
#'
#' @name context
#' @examples
#' df <- tibble(
#' g = sample(rep(letters[1:3], 1:3)),
#' x = runif(6),
#' y = runif(6)
#' )
#' gf <- df |> group_by(g)
#'
#' gf |> summarise(n = n())
#'
#' gf |> mutate(id = cur_group_id())
#' gf |> reframe(row = cur_group_rows())
#' gf |> summarise(data = list(cur_group()))
#'
#' gf |> mutate(across(everything(), ~ paste(cur_column(), round(.x, 2))))
NULL
#' @rdname context
#' @export
n <- function() {
peek_mask()$get_current_group_size()
}
#' @rdname context
#' @export
cur_group <- function() {
peek_mask()$current_key()
}
#' @rdname context
#' @export
cur_group_id <- function() {
peek_mask()$get_current_group_id()
}
#' @rdname context
#' @export
cur_group_rows <- function() {
peek_mask()$current_rows()
}
group_labels_details <- function(keys) {
keys <- map_chr(keys, pillar::format_glimpse)
labels <- vec_paste0(names(keys), " = ", keys)
labels <- cli_collapse(labels, last = ", ", sep2 = ", ")
cli::format_inline("{.code {labels}}")
}
cur_group_label <- function(
type = mask_type(),
id = cur_group_id(),
group = cur_group()
) {
switch(
type,
ungrouped = "",
grouped = glue("group {id}: {label}", label = group_labels_details(group)),
rowwise = glue("row {id}"),
stop_mask_type(type)
)
}
cur_group_data <- function(mask_type) {
switch(
mask_type,
ungrouped = list(),
grouped = list(id = cur_group_id(), group = cur_group()),
rowwise = list(id = cur_group_id()),
stop_mask_type(mask_type)
)
}
stop_mask_type <- function(type) {
cli::cli_abort(
"Unexpected mask type {.val {type}}.",
.internal = TRUE
)
}
cnd_data <- function(cnd, ctxt, mask, call) {
mask_type <- mask_type(mask)
has_group_data <- has_active_group_context(mask)
if (has_group_data) {
group_data <- cur_group_data(mask_type)
} else {
group_data <- NULL
}
list(
cnd = cnd,
name = ctxt$error_name,
expr = ctxt$error_expr,
type = mask_type,
has_group_data = has_group_data,
group_data = group_data,
call = call
)
}
#' @rdname context
#' @export
cur_column <- function() {
peek_column()
}
# context accessors -------------------------------------------------------
context_env <- new_environment()
context_poke <- function(name, value) {
old <- context_env[[name]]
context_env[[name]] <- value
old
}
context_peek_bare <- function(name) {
context_env[[name]]
}
context_peek <- function(name, location, call = caller_env()) {
context_peek_bare(name) %||%
abort(glue("Must only be used inside {location}."), call = call)
}
context_local <- function(name, value, frame = caller_env()) {
old <- context_poke(name, value)
# FIXME: Pass `after = TRUE` once we depend on R 3.5. Currently this
# doesn't restore in the correct order (FIFO) when context-local
# functions are called multiple times within the same frame.
expr <- expr(on.exit(context_poke(!!name, !!old), add = TRUE))
eval_bare(expr, frame)
value
}
peek_column <- function(call = caller_env()) {
context_peek("column", "`across()`", call)
}
local_column <- function(x, frame = caller_env()) {
context_local("column", x, frame = frame)
}
peek_mask <- function(call = caller_env()) {
context_peek(
"mask",
"data-masking verbs like `mutate()`, `filter()`, and `group_by()`",
call
)
}
local_mask <- function(x, frame = caller_env()) {
context_local("mask", x, frame = frame)
}