Skip to content

Commit 90df932

Browse files
committed
Prefer vec_rbind() over bind_rows()
1 parent 2f1df76 commit 90df932

15 files changed

+30
-56
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ importFrom(rlang,is_installed)
5252
importFrom(rlang,seq2)
5353
importFrom(rlang,set_names)
5454
importFrom(rlang,warn)
55+
importFrom(vctrs,vec_rbind)

R/compat-dplyr.R

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,6 @@ arrange_pos_id <- function(data) {
2424
data
2525
}
2626

27-
bind_rows <- function(x, y = NULL, ...) {
28-
if (is.null(x) && is.null(y)) {
29-
return(new_styler_df(list()))
30-
}
31-
if (is.null(x)) {
32-
if (inherits(y, "data.frame")) {
33-
return(y)
34-
}
35-
return(do.call(rbind.data.frame, x))
36-
}
37-
if (is.null(y)) {
38-
if (inherits(x, "data.frame")) {
39-
return(x)
40-
}
41-
return(do.call(rbind.data.frame, x))
42-
}
43-
if (NCOL(x) != NCOL(y)) {
44-
for (nme in setdiff(names(x), names(y))) {
45-
y[[nme]] <- NA
46-
}
47-
}
48-
bind_rows(rbind.data.frame(x, y), ...)
49-
}
50-
5127
filter <- function(.data, ...) {
5228
subset(.data, ...)
5329
}
@@ -80,9 +56,8 @@ slice <- function(.data, ...) {
8056
.data[c(...), , drop = FALSE]
8157
}
8258

83-
# TODO: Use `purrr::map_dfr()` when it stops implicitly relying on `{dplyr}`
84-
map_dfr <- function(.x, .f, ..., .id = NULL) {
59+
map_dfr <- function(.x, .f, ...) {
8560
.f <- purrr::as_mapper(.f, ...)
8661
res <- map(.x, .f, ...)
87-
bind_rows(res, .id = .id)
62+
vec_rbind(!!!res)
8863
}

R/nest.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,12 @@ nest_parse_data <- function(pd_flat) {
367367
#' the correct order.
368368
#' @param child A parse table or `NULL`.
369369
#' @param internal_child A parse table or `NULL`.
370-
#' @details Essentially, this is a wrapper around [dplyr::bind_rows()], but
371-
#' returns `NULL` if the result of [dplyr::bind_rows()] is a data frame with
370+
#' @details Essentially, this is a wrapper around vctrs::vec_rbind()], but
371+
#' returns `NULL` if the result of vctrs::vec_rbind()] is a data frame with
372372
#' zero rows.
373373
#' @keywords internal
374374
combine_children <- function(child, internal_child) {
375-
bound <- bind_rows(child, internal_child)
375+
bound <- vec_rbind(child, internal_child)
376376
if (nrow(bound) == 0L) {
377377
return(NULL)
378378
}

R/parse.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ ensure_correct_txt <- function(pd, text) {
167167
names(new_text),
168168
paste0(line_col_names(), "parent")
169169
)
170-
bind_rows(
170+
vec_rbind(
171171
new_text[, names_to_keep],
172172
pd[is_unaffected_token, ],
173173
pd[is_parent_of_problematic_string, ]

R/reindent.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ set_regex_indention <- function(flattened_pd,
108108
flatten_int()
109109

110110
to_check$lag_spaces[indices_to_force] <- target_indention
111-
bind_rows(to_check, not_to_check) %>%
111+
vec_rbind(to_check, not_to_check) %>%
112112
arrange_pos_id()
113113
}

R/relevel.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ flatten_pd <- function(pd_nested, token, child_token = token, left = TRUE) {
8686
bind_with_child <- function(pd_nested, pos) {
8787
pd_nested %>%
8888
slice(-pos) %>%
89-
bind_rows(pd_nested$child[[pos]]) %>%
89+
vec_rbind(pd_nested$child[[pos]]) %>%
9090
arrange_pos_id()
9191
}
9292

@@ -228,7 +228,7 @@ relocate_eq_assign_one <- function(pd) {
228228
eq_expr$parent <- NA
229229
pd$indent <- NULL
230230
non_eq_expr <- pd[-eq_ind, ]
231-
pd <- bind_rows(eq_expr, non_eq_expr) %>%
231+
pd <- vec_rbind(eq_expr, non_eq_expr) %>%
232232
arrange_pos_id()
233233
pd
234234
}

R/rules-tokens.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ add_brackets_in_pipe_one <- function(pd, pos) {
5151
block = NA,
5252
is_cached = FALSE
5353
)
54-
pd$child[[next_non_comment]] <- bind_rows(
55-
pd$child[[next_non_comment]],
56-
new_pd
57-
) %>%
54+
pd$child[[next_non_comment]] <- vec_rbind(pd$child[[next_non_comment]], new_pd) %>%
5855
arrange_pos_id()
5956
}
6057
pd
@@ -170,7 +167,7 @@ wrap_subexpr_in_curly <- function(pd,
170167

171168
pd %>%
172169
slice(-ind_to_be_wrapped) %>%
173-
bind_rows(new_expr_in_expr) %>%
170+
vec_rbind(new_expr_in_expr) %>%
174171
set_multi_line() %>%
175172
arrange_pos_id()
176173
}
@@ -204,7 +201,7 @@ fix_quotes <- function(pd_flat) {
204201
return(pd_flat)
205202
}
206203

207-
pd_flat$text[str_const] <- map(pd_flat$text[str_const], fix_quotes_one)
204+
pd_flat$text[str_const] <- map_chr(pd_flat$text[str_const], fix_quotes_one)
208205
pd_flat
209206
}
210207

R/styler-package.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#' style_text("a%>%b; a", scope = "tokens")
1919
"_PACKAGE"
2020

21-
## styler namespace: start
21+
## usethis namespace: start
2222
#'
23-
#' @importFrom rlang abort warn seq2 is_installed "%||%" set_names
24-
#' @importFrom purrr map map_lgl map_int map_chr map2 map2_chr map_at pmap pwalk
25-
#' @importFrom purrr compact partial flatten flatten_int flatten_chr
2623
#' @importFrom magrittr "%>%"
27-
#'
28-
## styler namespace: end
24+
#' @importFrom purrr compact partial flatten flatten_int flatten_chr
25+
#' @importFrom purrr map map_lgl map_int map_chr map2 map2_chr map_at pmap pwalk
26+
#' @importFrom rlang abort warn seq2 is_installed "%||%" set_names
27+
#' @importFrom vctrs vec_rbind
28+
## usethis namespace: end
2929
NULL
3030

3131

R/token-create.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,6 @@ wrap_expr_in_curly <- function(pd,
191191
indents = pd$indent[1L]
192192
)
193193

194-
bind_rows(opening, pd, closing) %>%
194+
vec_rbind(opening, pd, closing) %>%
195195
set_multi_line()
196196
}

R/unindent.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ set_unindention_child <- function(pd, token = "')'", unindent_by) {
3232
unindent_by = abs(pd$indent[closing] - pd$indent[closing - 1L])
3333
)
3434

35-
bind_rows(candidates, non_candidates) %>%
35+
vec_rbind(candidates, non_candidates) %>%
3636
arrange_pos_id()
3737
}
3838

0 commit comments

Comments
 (0)