Skip to content

Commit

Permalink
Support arbitrary vectors in enframe()
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jul 19, 2020
1 parent a78aea1 commit 27d1983
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 18 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Imports:
pkgconfig,
rlang (>= 0.4.3),
utils,
vctrs (>= 0.2.4)
vctrs (>= 0.3.2.9000)
Suggests:
bench,
bit64,
Expand All @@ -62,6 +62,8 @@ Suggests:
testthat (>= 2.1.0),
tidyr,
withr
Remotes:
r-lib/vctrs#1194
VignetteBuilder:
knitr
Encoding: UTF-8
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ importFrom(vctrs,vec_as_names_legacy)
importFrom(vctrs,vec_as_subscript2)
importFrom(vctrs,vec_c)
importFrom(vctrs,vec_is)
importFrom(vctrs,vec_names)
importFrom(vctrs,vec_names2)
importFrom(vctrs,vec_rbind)
importFrom(vctrs,vec_recycle)
importFrom(vctrs,vec_set_names)
importFrom(vctrs,vec_size)
importFrom(vctrs,vec_slice)
useDynLib(tibble, .registration = TRUE)
22 changes: 14 additions & 8 deletions R/enframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ enframe <- function(x, name = "name", value = "value") {
cnd_signal(error_enframe_value_null())
}

if (length(dim(x)) > 1) {
cnd_signal(error_enframe_has_dim(x))
if (is_null(x)) {
x <- logical()
}

if (is.null(x)) x <- logical()
if (!vec_is(x)) {
cnd_signal(error_enframe_must_be_vector(x))
}

if (is.null(name)) {
df <- list(unname(x))
} else if (is.null(names(x))) {
df <- list(vec_set_names(x, NULL))
} else if (is.null(vec_names(x))) {
df <- list(seq_along(x), x)
} else {
df <- list(names(x), unname(x))
df <- list(vec_names2(x), vec_set_names(x, NULL))
}

names(df) <- c(name, value)
Expand Down Expand Up @@ -67,10 +69,14 @@ deframe <- function(x) {
value
}

# Errors ------------------------------------------------------------------

error_enframe_value_null <- function() {
tibble_error("`value` can't be NULL.")
}

error_enframe_has_dim <- function(x) {
tibble_error(paste0("`x` must not have more than one dimension. `length(dim(x))` must be zero or one, not ", length(dim(x)), "."))
error_enframe_must_be_vector <- function(x) {
tibble_error(paste0(
"The `x` argument to `enframe()` must be a vector, not ", class(x)[[1]], "."
))
}
2 changes: 1 addition & 1 deletion R/tibble-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @import ellipsis
#' @importFrom vctrs vec_as_location vec_as_location2 vec_as_names vec_as_names_legacy vec_c
#' @importFrom vctrs vec_is vec_rbind vec_recycle vec_size vec_slice vec_slice<-
#' @importFrom vctrs unspecified vec_as_subscript2
#' @importFrom vctrs unspecified vec_as_subscript2 vec_names vec_names2 vec_set_names
#' @aliases NULL tibble-package
#' @details
#' `r lifecycle::badge("stable")`
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/enframe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
Error: `value` can't be NULL.

> enframe(Titanic)
Error: `x` must not have more than one dimension. `length(dim(x))` must be zero or one, not 4.
Error: Can't subset elements that don't exist.
x Locations 5, 6, 7, 8, 9, etc. don't exist.
i There are only 4 elements.

5 changes: 5 additions & 0 deletions tests/testthat/helper-zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ skip_enh_bullets_format <- function() {
skip_legacy()
}

skip_enh_enframe_vector <- function() {
# ENH: enframe() supports all vectors (#730)
skip_legacy()
}

skip_int_error_unknown_names <- function() {
# INT: error_unknown_names() no longer implemented
skip_legacy()
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/msg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ Use .name_repair to specify repair.
<error/tibble_error_enframe_value_null>
`value` can't be NULL.

> error_enframe_has_dim(Titanic)
<error/tibble_error_enframe_has_dim>
`x` must not have more than one dimension. `length(dim(x))` must be zero or one, not 4.
> error_enframe_must_be_vector(lm(speed ~ ., cars))
<error/tibble_error_enframe_must_be_vector>
The `x` argument to `enframe()` must be a vector, not lm.


glimpse
Expand Down
22 changes: 19 additions & 3 deletions tests/testthat/test-enframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,26 @@ test_that("can't use value = NULL", {
)
})

test_that("can't pass objects with dimensions", {
test_that("can pass vectors (#730)", {
a <- c(x = 1)
b <- data.frame(bb = 1, row.names = "y")
c <- matrix(1, dimnames = list(rows = "z", cols = "cc"))

au <- c(1)
bu <- data.frame(bb = 1)
cu <- matrix(1, dimnames = list(rows = NULL, cols = "cc"))

expect_identical(enframe(a, name = NULL), tibble(value = au))
expect_identical(enframe(b, name = NULL), tibble(value = bu))
expect_identical(enframe(c, name = NULL), tibble(value = cu))

expect_identical(enframe(a), tibble(name = "x", value = au))
expect_identical(enframe(b), tibble(name = "y", value = bu))
expect_identical(enframe(c), tibble(name = "z", value = cu))

expect_tibble_error(
enframe(iris),
error_enframe_has_dim(iris)
enframe(lm(speed ~ ., cars)),
error_enframe_must_be_vector(lm(speed ~ ., cars))
)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-msg.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ verify_output("msg.txt", {
"#enframe"
error_enframe_value_null()

error_enframe_has_dim(Titanic)
error_enframe_must_be_vector(lm(speed ~ ., cars))

"# glimpse"
error_glimpse_infinite_width()
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-zzz-enframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ test_that("can't use value = NULL", {
})

test_that("can't pass objects with dimensions", {
skip_enh_enframe_vector()

expect_legacy_error(
enframe(iris),
error_enframe_has_dim(iris),
Expand Down

0 comments on commit 27d1983

Please sign in to comment.