Skip to content

Commit

Permalink
flatten should silently drop NULLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Dec 29, 2015
1 parent be22add commit 13ba73a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SEXP flatten_impl(SEXP x) {

for (int j = 0; j < m; ++j) {
SEXP x_j = VECTOR_ELT(x, j);
if (!Rf_isVector(x_j))
if (!Rf_isVector(x_j) && !Rf_isNull(x_j))
Rf_errorcall(R_NilValue, "Element %i is not a vector (%s)", j + 1, objtype(x_j));

n += Rf_length(x_j);
Expand Down Expand Up @@ -70,8 +70,6 @@ SEXP flatten_impl(SEXP x) {
if (i % 1000 == 0)
R_CheckUserInterrupt();
}


}


Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-flatten.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test_that("each second level element becomes first level element", {
expect_equal(flatten(list(1, 2)), list(1, 2))
})

test_that("NULLs are silently dropped", {
expect_equal(flatten(list(NULL, NULL)), list())
expect_equal(flatten(list(NULL, 1)), list(1))
expect_equal(flatten(list(1, NULL)), list(1))
})

test_that("names are preserved", {
expect_equal(flatten(list(list(x = 1), list(y = 1))), list(x = 1, y = 1))
expect_equal(flatten(list(list(a = 1, b = 2), 3)), list(a = 1, b = 2, 3))
Expand Down

0 comments on commit 13ba73a

Please sign in to comment.