Skip to content

Commit ac97b3e

Browse files
authored
Merge pull request #905 from tidyverse/f-797-as-tibble-row-arbitrary
- `as_tibble_row()` supports arbitrary vectors (#797).
2 parents de2abba + da7e4e3 commit ac97b3e

File tree

6 files changed

+62
-26
lines changed

6 files changed

+62
-26
lines changed

R/as_tibble.R

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,7 @@ as_tibble.default <- function(x, ...) {
270270

271271
#' @description
272272
#' `as_tibble_row()` converts a vector to a tibble with one row.
273-
#' The input must be a bare vector, e.g. vectors of dates are not
274-
#' supported yet.
275-
#' If the input is a list, all elements must have length one.
273+
#' If the input is a list, all elements must have size one.
276274
#'
277275
#' @rdname as_tibble
278276
#' @export
@@ -284,15 +282,24 @@ as_tibble.default <- function(x, ...) {
284282
as_tibble_row <- function(x,
285283
.name_repair = c("check_unique", "unique", "universal", "minimal")) {
286284

287-
if (!is_bare_vector(x)) {
288-
# FIXME: Remove entry from help once fixed (#797)
289-
cnd_signal(error_as_tibble_row_bare(x))
285+
if (!vec_is(x)) {
286+
cnd_signal(error_as_tibble_row_vector(x))
290287
}
291288

292-
x <- set_repaired_names(x, repair_hint = TRUE, .name_repair)
289+
names <- vectbl_names2(x, .name_repair = .name_repair)
290+
291+
# FIXME: Use vec_chop2() when https://github.com/r-lib/vctrs/pull/1226 is in
292+
if (is_bare_list(x)) {
293+
slices <- x
294+
} else {
295+
x <- vec_set_names(x, NULL)
296+
slices <- lapply(seq_len(vec_size(x)), vec_slice, x = x)
297+
names(slices) <- names
298+
}
299+
300+
check_all_lengths_one(slices)
293301

294-
check_all_lengths_one(x)
295-
new_tibble(as.list(x), nrow = 1)
302+
new_tibble(slices, nrow = 1)
296303
}
297304

298305
check_all_lengths_one <- function(x) {
@@ -346,9 +353,9 @@ error_column_scalar_type <- function(names, positions, classes) {
346353
)
347354
}
348355

349-
error_as_tibble_row_bare <- function(x) {
356+
error_as_tibble_row_vector <- function(x) {
350357
tibble_error(paste0(
351-
"`x` must be a bare vector in `as_tibble_row()`, not ", class(x)[[1]], "."
358+
"`x` must be a vector in `as_tibble_row()`, not ", class(x)[[1]], "."
352359
))
353360
}
354361

R/names.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
vectbl_names2 <- function(x,
2+
.name_repair = c("check_unique", "unique", "universal", "minimal"),
3+
quiet = FALSE) {
4+
5+
name <- vec_names2(x, repair = "minimal", quiet = quiet)
6+
repaired_names(name, repair_hint = TRUE, .name_repair = .name_repair, quiet = quiet)
7+
}
8+
19
set_repaired_names <- function(x,
210
repair_hint,
311
.name_repair = c("check_unique", "unique", "universal", "minimal"),

man/as_tibble.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/msg.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@
9090
x Column `C` is c.
9191
x ... and 23 more problems.
9292
Code
93-
error_as_tibble_row_bare(new_environment())
93+
error_as_tibble_row_vector(new_environment())
9494
Output
95-
<error/tibble_error_as_tibble_row_bare>
96-
`x` must be a bare vector in `as_tibble_row()`, not environment.
95+
<error/tibble_error_as_tibble_row_vector>
96+
`x` must be a vector in `as_tibble_row()`, not environment.
9797
Code
9898
error_as_tibble_row_size_one(3, "foo", 7)
9999
Output

tests/testthat/test-as_tibble.R

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,18 +416,41 @@ test_that("as_tibble_row() can convert named bare vectors to data frame", {
416416
)
417417
})
418418

419-
test_that("as_tibbe_row() fails with non-bare vectors (#739)", {
419+
test_that("as_tibble_row() works with non-bare vectors (#797)", {
420420
expect_tibble_error(
421-
as_tibble_row(Sys.time()),
422-
error_as_tibble_row_bare(Sys.time())
421+
as_tibble_row(new_environment()),
422+
error_as_tibble_row_vector(new_environment())
423423
)
424-
expect_tibble_error(
425-
as_tibble_row(iris),
426-
error_as_tibble_row_bare(iris)
424+
425+
time <- vec_slice(Sys.time(), 1)
426+
expect_identical(
427+
as_tibble_row(time, .name_repair = "unique"),
428+
tibble(...1 = time)
427429
)
428-
expect_tibble_error(
430+
expect_identical(
431+
as_tibble_row(trees[1:3, ], .name_repair = "unique"),
432+
tibble(
433+
...1 = remove_rownames(trees[1, ]),
434+
...2 = remove_rownames(trees[2, ]),
435+
...3 = remove_rownames(trees[3, ])
436+
)
437+
)
438+
439+
remove_first_dimname <- function(x) {
440+
dn <- dimnames(x)
441+
dn[1] <- list(NULL)
442+
dimnames(x) <- dn
443+
x
444+
}
445+
446+
expect_identical(
429447
as_tibble_row(Titanic),
430-
error_as_tibble_row_bare(Titanic)
448+
tibble(
449+
"1st" = remove_first_dimname(Titanic[1,,,, drop = FALSE]),
450+
"2nd" = remove_first_dimname(Titanic[2,,,, drop = FALSE]),
451+
"3rd" = remove_first_dimname(Titanic[3,,,, drop = FALSE]),
452+
Crew = remove_first_dimname(Titanic[4,,,, drop = FALSE])
453+
)
431454
)
432455
})
433456

tests/testthat/test-msg.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test_that("output test", {
3030
error_column_scalar_type(letters[2:3], 3:4, c("name", "NULL"))
3131
error_column_scalar_type(c("", "", LETTERS), 1:28, c("QQ", "VV", letters))
3232

33-
error_as_tibble_row_bare(new_environment())
33+
error_as_tibble_row_vector(new_environment())
3434
error_as_tibble_row_size_one(3, "foo", 7)
3535

3636
"# class-tbl_df"

0 commit comments

Comments
 (0)