Skip to content

Commit

Permalink
Better error message for array_branch on vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Nov 28, 2018
1 parent da5a769 commit f3f3da0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion R/arrays.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ array_branch <- function(array, margin = NULL) {

if (length(margin) == 0) {
list(array)
} else if (identical(as.integer(margin), 1L) && is.null(dim(array))) {
} else if (is.null(dim(array))) {
if (!identical(as.integer(margin), 1L)) {
stop(
"The passed array is 1D and does not have the margin(s) ",
toString(margin), call. = FALSE)
}
as.list(array)
} else {
flatten(apply(array, margin, list))
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-arrays.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ test_that("array_branch works on vectors", {
expect_identical(array_branch(1:3, 1), list(1L, 2L, 3L))
})

test_that("array_branch throws an error for wrong margins on a vector", {
expect_error(array_branch(1:3, 2), "does not have the margin\\(s\\) 2")
})

test_that("length depends on whether list is flattened or not", {
m1 <- c(3, 1)
m2 <- 3
Expand Down

0 comments on commit f3f3da0

Please sign in to comment.