Skip to content

Commit

Permalink
Merge pull request #9 from robertzk/depth_0_lists
Browse files Browse the repository at this point in the history
Depth 0 lists
  • Loading branch information
peterhurford committed Oct 14, 2015
2 parents 01aabc5 + e036d32 commit ea71fed
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Type: Package
Title: recombinator (http://github.com/robertzk/recombinator)
Description: R tools for turning nested lists into data.frames
in an orderly manner.
Version: 0.1.2
Version: 0.1.3
Author: Robert Krzyzanowski <technoguyrob@gmail.com>
Maintainer: Robert Krzyzanowski <technoguyrob@gmail.com>
Authors@R: c(person("Robert", "Krzyzanowski",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 0.1.3

* Recombinator calls out to homogeneous recombinator correctly for depth-0 lists. Resolves #8.

# Version 0.1.2

* Recombinator calls out to homogeneous recombinator correctly when the names are a list within a list (instead of a vector). Resolves #6.
Expand Down
24 changes: 21 additions & 3 deletions R/recombinator.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,37 @@
recombinator <- function(dat, id = "id") {
if (!is.list(dat) || is.data.frame(dat)) {
dat
} else if (is.list(dat[[1L]]) && (has_names(dat) || has_names(dat[[1L]]))) {
} else if (is_heterogeneous(dat)) {
heterogeneous_recombinator(dat, id)
} else if (length(dat[[1L]]) > 1) {
} else if (is_homogeneous(dat)) {
homogeneous_recombinator(dat, id)
} else {
stop("Invalid recombinator format: pass either (1) ",
"a list whose first element is a character vector of names",
"a list whose first element is a character vector of names ",
"and the subsequent list elements are unnamed lists of values ",
"or (2) a list each of whose elements are named lists.", call. = FALSE)
}
}


#' Is this heterogeneous data?
#' @param dat list. The list to verify.
#' @return boolean. TRUE if the list is heterogeneous, FALSE otherwise.
is_heterogeneous <- function(dat) {
is.list(dat[[1L]]) && (has_names(dat) || has_names(dat[[1L]]))
}

#' Is this homogeneous data?
#' @param dat list. The list to verify.
#' @return boolean. TRUE if the list is heterogeneous, FALSE otherwise.
is_homogeneous <- function(dat) {
if (is_heterogeneous(dat)) { FALSE }
else if (is.character(dat[[1L]]) && !("list" %in% sapply(dat, class))) { TRUE }
else if (length(dat[[1L]]) > 1) { TRUE }
else { FALSE }
}


#' Checks if a list has names.
#'
#' @param dat list. The list to verify.
Expand Down
13 changes: 11 additions & 2 deletions tests/testthat/test-recombinators.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ test_that("recombinator calls to homogeneous recombinator correctly #2. Issue #
expect_identical(recombinator(pre_dataframe), df)
})

test_that("recombinator calls to homogeneous recombinator for depth-0 lists. Issue #8", {
pre_dataframe <- list("title", 1, 2, 3)
df <- data.frame(title = c(1, 2, 3))
expect_identical(recombinator(pre_dataframe), df)
})

test_that("recombinator doesn't recombinate a depth-0 list with no first name.", {
pre_dataframe <- list(1, 2, 3)
expect_error(recombinator(pre_dataframe))
})

test_that("names that are different than make.names are nonstandard", {
expect_warning(warn_on_nonstandard_names(list(`x:2` = 2)))
})
Expand All @@ -64,5 +75,3 @@ test_that("a list with no names is not unstandard", {
test_that("recombinator warns about nonstandard names", {
expect_warning(homogeneous_recombinator(list(`x:2` = 2)))
})


0 comments on commit ea71fed

Please sign in to comment.