diff --git a/R/arrays.R b/R/arrays.R index 1482bb1c..01bb1ffd 100644 --- a/R/arrays.R +++ b/R/arrays.R @@ -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)) diff --git a/tests/testthat/test-arrays.R b/tests/testthat/test-arrays.R index ee01aefb..3f5a0a4d 100644 --- a/tests/testthat/test-arrays.R +++ b/tests/testthat/test-arrays.R @@ -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