Skip to content

Commit

Permalink
Snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Mar 20, 2020
1 parent 5e811c9 commit 069f89e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
34 changes: 27 additions & 7 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") {
abort(error_enframe_value_null())
}

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

if (is_null(x)) x <- logical()
if (!vec_is(x)) {
abort(error_enframe_must_be_vector(x))
}

if (is_null(name)) {
df <- list(unname(x))
df <- list(vectbl_strip_names(x))
} else if (is_null(names(x))) {
df <- list(seq_along(x), x)
} else {
df <- list(names(x), unname(x))
df <- list(vectbl_names(x), vectbl_strip_names(x))
}

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

vectbl_names <- function(x) {
maybe_row_names <- is.data.frame(x) || is.array(x)

if (is.array(x)) {
row.names(x)
} else if (is.data.frame(x)) {
if (has_rownames(x)) {
rownames(x)
} else {
NULL
}
} else {
names(x)
}
}

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

error_enframe_value_null <- function() {
tibble_error("The `value` argument to `enframe()` cannot 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]]))
}
5 changes: 5 additions & 0 deletions tests/testthat/helper-zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ skip_enh_as_tibble_retired <- 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 @@ -2,9 +2,9 @@
<error/tibble_error_enframe_value_null>
The `value` argument to `enframe()` cannot 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

> error_need_rhs_vector(quote(RHS))
<error/tibble_error_need_rhs_vector>
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 @@ -11,7 +11,7 @@ test_that("aborting with class", {
verify_output("msg.txt", {
error_enframe_value_null()

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

error_need_rhs_vector(quote(RHS))
error_need_rhs_vector_or_null(quote(RHS))
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 069f89e

Please sign in to comment.