Skip to content

Commit eba7b89

Browse files
committed
.id bug in bind_rows()
1 parent 030f159 commit eba7b89

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

R/bind-rows.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ bind_rows <- function(..., .id = NULL) {
6565
check_string(.id)
6666

6767
if (!is_named(dots)) {
68-
names(dots) <- seq_along(dots)
68+
vec_present_names <- rlang::have_name(dots)
69+
vec_missing_indices <- which(vec_present_names == FALSE)
70+
names(dots)[vec_missing_indices] <- vec_missing_indices
6971
}
7072
} else {
7173
# Don't let `vec_rbind(.id = NULL)` promote input names to row names

tests/testthat/test-bind-rows.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ test_that("bind_rows() handles named list", {
145145
expect_equal(bind_rows(x), tibble(x = 1, y = 2, z = 3))
146146
})
147147

148+
test_that("bind_rows() handles empty names in a list", {
149+
x <- lapply(1:5, function(x) mtcars)
150+
names(x) = paste0("id_", 1:5)
151+
names(x)[c(3, 5)] = NA_character_
152+
x <- bind_rows(x, .id = "id")
153+
x_ids <- x$id |> unique()
154+
155+
# If names are missing, bind_rows will replace with index#
156+
expect_equal(x_ids, c("id_1", "id_2", "3", "id_4", "5"))
157+
})
158+
148159
test_that("bind_rows() validates lists (#5417)", {
149160
out <- bind_rows(list(x = 1), list(x = 1, y = 1:2))
150161
expect_equal(out, tibble(x = c(1, 1, 1), y = c(NA, 1:2)))

0 commit comments

Comments
 (0)