@@ -166,84 +166,7 @@ join_keys <- function(x, y, by) {
166166 list (x = keys [seq_len(n_x )], y = keys [n_x + seq_len(n_y )],
167167 n = attr(keys , " n" ))
168168}
169- # ' Replace specified values with new values, in a factor or character vector
170- # '
171- # ' An easy to use substitution of elements in a string-like vector (character or
172- # ' factor). If `x` is a character vector the matching elements will be replaced
173- # ' directly and if `x` is a factor the matching levels will be replaced
174- # '
175- # ' @param x A character or factor vector
176- # ' @param replace A named character vector with the names corresponding to the
177- # ' elements to replace and the values giving the replacement.
178- # '
179- # ' @return A vector of the same class as `x` with the given values replaced
180- # '
181- # ' @keywords internal
182- # ' @noRd
183- # '
184- revalue <- function (x , replace ) {
185- if (is.character(x )) {
186- replace <- replace [names(replace ) %in% x ]
187- if (length(replace ) == 0 ) return (x )
188- x [match(names(replace ), x )] <- replace
189- } else if (is.factor(x )) {
190- lev <- levels(x )
191- replace <- replace [names(replace ) %in% lev ]
192- if (length(replace ) == 0 ) return (x )
193- lev [match(names(replace ), lev )] <- replace
194- levels(x ) <- lev
195- } else if (! is.null(x )) {
196- stop_input_type(x , " a factor or character vector" )
197- }
198- x
199- }
200- # Iterate through a formula and return a quoted version
201- simplify_formula <- function (x ) {
202- if (length(x ) == 2 && x [[1 ]] == as.name(" ~" )) {
203- return (simplify(x [[2 ]]))
204- }
205- if (length(x ) < 3 )
206- return (list (x ))
207- op <- x [[1 ]]
208- a <- x [[2 ]]
209- b <- x [[3 ]]
210- if (op == as.name(" +" ) || op == as.name(" *" ) || op ==
211- as.name(" ~" )) {
212- c(simplify(a ), simplify(b ))
213- }
214- else if (op == as.name(" -" )) {
215- c(simplify(a ), bquote(- .(x ), list (x = simplify(b ))))
216- }
217- else {
218- list (x )
219- }
220- }
221- # ' Create a quoted version of x
222- # '
223- # ' This function captures the special meaning of formulas in the context of
224- # ' facets in ggplot2, where `+` have special meaning. It works as
225- # ' `plyr::as.quoted` but only for the special cases of `character`, `call`, and
226- # ' `formula` input as these are the only situations relevant for ggplot2.
227- # '
228- # ' @param x A formula, string, or call to be quoted
229- # ' @param env The environment to a attach to the quoted expression.
230- # '
231- # ' @keywords internal
232- # ' @noRd
233- # '
234- as.quoted <- function (x , env = parent.frame()) {
235- x <- if (is.character(x )) {
236- lapply(x , function (x ) parse(text = x )[[1 ]])
237- } else if (is.formula(x )) {
238- simplify_formula(x )
239- } else if (is.call(x )) {
240- as.list(x )[- 1 ]
241- } else {
242- cli :: cli_abort(" Must be a character vector, call, or formula." )
243- }
244- attributes(x ) <- list (env = env , class = ' quoted' )
245- x
246- }
169+
247170# round a number to a given precision
248171round_any <- function (x , accuracy , f = round ) {
249172 check_numeric(x )
@@ -286,29 +209,20 @@ dapply <- function(df, by, fun, ..., drop = TRUE) {
286209 }
287210
288211 # Shortcut when only one group
289- if (all(vapply(grouping_cols , single_value , logical (1 )))) {
212+ has_single_group <- all(vapply(
213+ grouping_cols ,
214+ function (x ) identical(as.character(levels(x ) %|| % attr(x , " n" )), " 1" ),
215+ logical (1 )
216+ ))
217+ if (has_single_group ) {
290218 return (apply_fun(df ))
291219 }
292220
293221 ids <- id(grouping_cols , drop = drop )
294222 group_rows <- split_with_index(seq_len(nrow(df )), ids )
295223 result <- lapply(seq_along(group_rows ), function (i ) {
296- cur_data <- df_rows (df , group_rows [[i ]])
224+ cur_data <- vec_slice (df , group_rows [[i ]])
297225 apply_fun(cur_data )
298226 })
299227 vec_rbind0(!!! result )
300228}
301-
302- single_value <- function (x , ... ) {
303- UseMethod(" single_value" )
304- }
305- # ' @export
306- single_value.default <- function (x , ... ) {
307- # This is set by id() used in creating the grouping var
308- identical(attr(x , " n" ), 1L )
309- }
310- # ' @export
311- single_value.factor <- function (x , ... ) {
312- # Panels are encoded as factor numbers and can never be missing (NA)
313- identical(levels(x ), " 1" )
314- }
0 commit comments