diff --git a/NEWS.md b/NEWS.md index fd248dd..4d1e661 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # Unreleased version +- List-columns may now be passed to `timevis()`, to support `nestedGroups` and any other parameters that may require nested lists - Support `subgroupStack = TRUE` option (#117) - Don't use local images in demo app (#118) diff --git a/R/utils.R b/R/utils.R index 7f9ca2f..e668e06 100644 --- a/R/utils.R +++ b/R/utils.R @@ -24,12 +24,11 @@ dataframeToD3 <- function(df) { row.names(df) <- NULL lapply(seq_len(nrow(df)), function(row) { row <- df[row, , drop = FALSE] - lapply(row[, !is.na(row), drop = FALSE], function(x) { - if (!is.logical(x)) { - as.character(x) - } else { - x - } + lapply(row[, !is.na(row), drop = FALSE], function(x){ + if (!lengths(x)) return(NA) + if (is.logical(x)) return(x) + if (lengths(x) > 1 | is.list(x)) return(lapply(unlist(x),as.character)) + return(as.character(x)) }) }) } diff --git a/tests/testthat/setup_helpers.R b/tests/testthat/setup_helpers.R new file mode 100644 index 0000000..a18e9b3 --- /dev/null +++ b/tests/testthat/setup_helpers.R @@ -0,0 +1,9 @@ +# Produce a more informative output when failing expect_equivalent/identical +# testcases for visual comparison +info_comp <- function(actual, expect){ + paste0( + c("Expect: ", "Actual: "), + c(expect, actual), + collapse = "\n" + ) +} diff --git a/tests/testthat/test-dataframeToD3.R b/tests/testthat/test-dataframeToD3.R index 4a686c1..61a7fd0 100644 --- a/tests/testthat/test-dataframeToD3.R +++ b/tests/testthat/test-dataframeToD3.R @@ -8,21 +8,24 @@ test_that("dataframeToD3 works with no input", { test_that("dataframeToD3 works with a single row", { df <- data.frame(name = "Dean", age = 27, stringsAsFactors = FALSE) list <- list(list(name = "Dean", age = "27")) - expect_identical(dataframeToD3(df), list) + expect_identical(dataframeToD3(df), list, + info = info_comp(dataframeToD3(df), list)) }) test_that("dataframeToD3 works with multiple rows", { df <- data.frame(name = c("Dean", "Ben"), age = c(27, 24)) list <- list(list(name = "Dean", age = "27"), list(name = "Ben", age = "24")) - expect_identical(dataframeToD3(df), list) + expect_identical(dataframeToD3(df), list, + info = info_comp(dataframeToD3(df), list)) }) test_that("dataframeToD3 works with NA values", { df <- data.frame(name = c("Dean", "Ben"), age = c(27, 24), degree = c("MSc", NA)) list <- list(list(name = "Dean", age = "27", degree = "MSc"), list(name = "Ben", age = "24")) - expect_identical(dataframeToD3(df), list) + expect_identical(dataframeToD3(df), list, + info = info_comp(dataframeToD3(df), list)) }) test_that("dataframeToD3 errors when given a non-dataframe", { @@ -33,5 +36,36 @@ test_that("dataframeToD3 errors when given a non-dataframe", { test_that("dataframeToD3 returns the same whether the dataframe is pure or merged", { df <- data.frame(name = c("Dean", "Ben"), age = c(27, 24)) df_rbind <- rbind(df[1, ], df[2, ]) - expect_identical(dataframeToD3(df), dataframeToD3(df_rbind)) + expect_identical(dataframeToD3(df), dataframeToD3(df_rbind), + info = info_comp(dataframeToD3(df), dataframeToD3(df_rbind))) +}) + + +test_that("nested columns behave the way they ought to",{ + matts_hobbies <- c("Working", "thinking about work") + df <- data.frame(name = c("Dean", "Matt"), + age = c(27, 23), + hobbies = c(NA, NA)) + df$hobbies[2] <- list(matts_hobbies) + out <- dataframeToD3(df) + expect <- list(list(name = "Dean", age = "27"), + list(name = "Matt", age = "23", hobbies = as.list(matts_hobbies))) + expect_identical(out, expect, info = info_comp(out, expect)) + + df2 <- df + expect2 <- expect + + deans_hobby <- "Responding to pull requests" + df2$hobbies[[1]] <- list(deans_hobby) + expect2[[1]]$hobbies <- list(deans_hobby) + out2 <- dataframeToD3(df2) + expect_identical(out2, expect2, info = info_comp(out2, expect2)) + + df3 <- df2 + expect3 <- expect2 + expect3[[1]]$hobbies <- NA + + df3$hobbies[[1]] <- list() + out3 <- df3 %>% dataframeToD3() + expect_identical(out3, expect3, info = info_comp(out3, expect3)) })