forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeprec-do.r
248 lines (213 loc) · 6.88 KB
/
deprec-do.r
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#' Do anything
#'
#' @description
#' \Sexpr[results=rd, stage=render]{lifecycle::badge("deprecated")}
#'
#' `do()` is deprecated as of dplyr 1.0.0, because its syntax never really
#' felt like it belong with the rest of dplyr. It's replaced by a combination
#' of [summarise()] (which can now produce multiple rows and multiple columns),
#' [condense()] (which creates a [rowwise] tibble containing list-columns),
#' and [across()] (which allows you to access the data for the "current" group).
#'
#' @param .data a tbl
#' @param ... Expressions to apply to each group. If named, results will be
#' stored in a new column. If unnamed, should return a data frame. You can
#' use `.` to refer to the current group. You can not mix named and
#' unnamed arguments.
#' @keywords internal
#' @export
#' @examples
#' # do() with unnamed arguments becomes summarise()
#' # . becomes across()
#' by_cyl <- mtcars %>% group_by(cyl)
#' by_cyl %>% do(head(., 2))
#' # ->
#' by_cyl %>% summarise(head(across(), 2))
#' by_cyl %>% slice_head(n = 2)
#'
#' # Can refer to variables directly
#' by_cyl %>% do(mean = mean(.$vs))
#' # ->
#' by_cyl %>% condense(mean = mean(vs))
#'
#' # do() with named arguments becomes condense()
#' models <- by_cyl %>% do(mod = lm(mpg ~ disp, data = .))
#' # ->
#' models <- by_cyl %>% condense(mod = lm(mpg ~ disp))
#' models %>% summarise(rsq = summary(mod)$r.squared)
#'
#' # use broom to turn models into data
#' models %>% do(data.frame(
#' var = names(coef(.$mod)),
#' coef(summary(.$mod)))
#' )
#' # ->
#' if (requireNamespace("broom")) {
#' models %>% summarise(broom::tidy(mod))
#' }
do <- function(.data, ...) {
lifecycle::deprecate_warn("1.0.0", "do()",
details = "Use condense() or summarise()"
)
UseMethod("do")
}
#' @export
do.NULL <- function(.data, ...) {
NULL
}
#' @export
do.grouped_df <- function(.data, ...) {
index <- group_rows(.data)
labels <- select(group_data(.data), -last_col())
attr(labels, ".drop") <- NULL
# Create ungroup version of data frame suitable for subsetting
group_data <- ungroup(.data)
args <- enquos(...)
named <- named_args(args)
mask <- new_data_mask(new_environment())
n <- length(index)
m <- length(args)
# Special case for zero-group/zero-row input
if (n == 0) {
if (named) {
out <- rep_len(list(list()), length(args))
out <- set_names(out, names(args))
out <- label_output_list(labels, out, groups(.data))
} else {
env_bind_do_pronouns(mask, group_data)
out <- eval_tidy(args[[1]], mask)
out <- out[0, , drop = FALSE]
out <- label_output_dataframe(labels, list(list(out)), group_vars(.data), group_by_drop_default(.data))
}
return(out)
}
# Add pronouns with active bindings that resolve to the current
# subset. `_i` is found in environment of this function because of
# usual scoping rules.
group_slice <- function(value) {
if (missing(value)) {
group_data[index[[`_i`]], , drop = FALSE]
} else {
group_data[index[[`_i`]], ] <<- value
}
}
env_bind_do_pronouns(mask, group_slice)
out <- replicate(m, vector("list", n), simplify = FALSE)
names(out) <- names(args)
p <- progress_estimated(n * m, min_time = 2)
for (`_i` in seq_len(n)) {
for (j in seq_len(m)) {
out[[j]][`_i`] <- list(eval_tidy(args[[j]], mask))
p$tick()$print()
}
}
if (!named) {
label_output_dataframe(labels, out, group_vars(.data), group_by_drop_default(.data))
} else {
label_output_list(labels, out, group_vars(.data))
}
}
#' @export
do.data.frame <- function(.data, ...) {
args <- enquos(...)
named <- named_args(args)
# Create custom data mask with `.` pronoun
mask <- new_data_mask(new_environment())
env_bind_do_pronouns(mask, .data)
if (!named) {
out <- eval_tidy(args[[1]], mask)
if (!inherits(out, "data.frame")) {
bad("Result must be a data frame, not {fmt_classes(out)}")
}
} else {
out <- map(args, function(arg) list(eval_tidy(arg, mask)))
names(out) <- names(args)
out <- tibble::as_tibble(out, validate = FALSE)
}
out
}
# Helper functions -------------------------------------------------------------
env_bind_do_pronouns <- function(env, data) {
if (is_function(data)) {
bind <- env_bind_active
} else {
bind <- env_bind
}
# Use `:=` for `.` to avoid partial matching with `.env`
bind(env, "." := data, .data = data)
}
label_output_dataframe <- function(labels, out, groups, .drop) {
data_frame <- vapply(out[[1]], is.data.frame, logical(1))
if (any(!data_frame)) {
bad("Results {bad} must be data frames, not {first_bad_class}",
bad = fmt_comma(which(!data_frame)),
first_bad_class = fmt_classes(out[[1]][[which.min(data_frame)]])
)
}
rows <- vapply(out[[1]], nrow, numeric(1))
out <- bind_rows(out[[1]])
if (!is.null(labels)) {
# Remove any common columns from labels
labels <- labels[setdiff(names(labels), names(out))]
# Repeat each row to match data
labels <- labels[rep(seq_len(nrow(labels)), rows), , drop = FALSE]
rownames(labels) <- NULL
grouped_df(bind_cols(labels, out), groups, .drop)
} else {
rowwise(out)
}
}
label_output_list <- function(labels, out, groups) {
if (!is.null(labels)) {
labels[names(out)] <- out
rowwise(labels)
} else {
class(out) <- "data.frame"
attr(out, "row.names") <- .set_row_names(length(out[[1]]))
rowwise(out)
}
}
named_args <- function(args) {
# Arguments must either be all named or all unnamed.
named <- sum(names2(args) != "")
if (!(named == 0 || named == length(args))) {
abort("Arguments must either be all named or all unnamed")
}
if (named == 0 && length(args) > 1) {
bad("Can only supply one unnamed argument, not {length(args)}")
}
# Check for old syntax
if (named == 1 && names(args) == ".f") {
abort("do syntax changed in dplyr 0.2. Please see documentation for details")
}
named != 0
}
#' @export
do.rowwise_df <- function(.data, ...) {
# Create ungroup version of data frame suitable for subsetting
group_data <- ungroup(.data)
args <- enquos(...)
named <- named_args(args)
# Create new environment, inheriting from parent, with an active binding
# for . that resolves to the current subset. `_i` is found in environment
# of this function because of usual scoping rules.
mask <- new_data_mask(new_environment())
current_row <- function() lapply(group_data[`_i`, , drop = FALSE], "[[", 1)
env_bind_do_pronouns(mask, current_row)
n <- nrow(.data)
m <- length(args)
out <- replicate(m, vector("list", n), simplify = FALSE)
names(out) <- names(args)
p <- progress_estimated(n * m, min_time = 2)
for (`_i` in seq_len(n)) {
for (j in seq_len(m)) {
out[[j]][`_i`] <- list(eval_tidy(args[[j]], mask))
p$tick()$print()
}
}
if (!named) {
label_output_dataframe(NULL, out, groups(.data), group_by_drop_default(.data))
} else {
label_output_list(NULL, out, groups(.data))
}
}