From 5a3d079a117c6a083bf3bc65e621a29d4aafbaf8 Mon Sep 17 00:00:00 2001 From: Matthew Strasiotto <39424834+mstr3336@users.noreply.github.com> Date: Sun, 31 Mar 2019 17:42:53 +1100 Subject: [PATCH 1/4] Is able to past list-cols to timevis (#11) * Added glue to suggests * Changed encoding function to produce json objects * Added helper for more informative test output * Added calls to info output helper * Only writes list-cols as json Passes regression tests again * Added unit test to test nested col behaviour (Failing) * new test now passing * Now passes list-cols (such as nestedGroups) through * Update version number * Added .DS_Store to gitignore * Added newest release of vis.js * Update timevis.yaml to point at vis-4.21 * Added .DS_Store to .gitignore * Updated unit test * commented info comp test helper * Added unit test to check length-1 listing * Added list check to dataframetod3 * added null handle to dftod3 * Commented null handle out in dataframetoD3 Mainly because I should probably write more regression tests for it before blindly putting it in I should probably implement a check on the whole column, rather than checking in an apply to check if any are vectors in the col * Added timeline plus * Updated dependencies to use timeline-plus * renamed timeline-plus back to vis, setup src properly * Update timevis.yaml * Changed references from vis to timeline * Update timevis.yaml * Revert "Changed references from vis to timeline" This reverts commit d7c169e8b869c6b8b04f7a3d9fcd9a210b7d9049. * changed all refs to timeline to vistime because of collision * replaced references to vis with timeline * updated checks in dataframetod3 coercion added a check for logical * Replaced timeline.js with my own fixed version * Extra test case to cover when empty list passed * dftod3 Passes empty lists and null as NA * Revert "Merge pull request #7 from mstr3336/upgrade-timeline-plusplus" This reverts commit 38bd4e47772690dd649071edc855db2e76b1dad2, reversing changes made to 90501148f2120fec6f5f7f07b54fe815ff956b8f. --- .gitignore | 1 + DESCRIPTION | 3 ++- R/utils.R | 7 ++++- tests/testthat/setup_helpers.R | 10 +++++++ tests/testthat/test-dataframeToD3.R | 42 ++++++++++++++++++++++++++--- 5 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/setup_helpers.R diff --git a/.gitignore b/.gitignore index 09a72cb..db183f4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .Rhistory .RData inst/doc +.DS_Store diff --git a/DESCRIPTION b/DESCRIPTION index 6c8ea5c..36de0bf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,8 @@ Imports: Suggests: knitr (>= 1.7), testthat (>= 0.9.1), - tibble + tibble, + glue License: MIT + file LICENSE Encoding: UTF-8 LazyData: true diff --git a/R/utils.R b/R/utils.R index 7cc1013..2feba03 100644 --- a/R/utils.R +++ b/R/utils.R @@ -24,7 +24,12 @@ 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], as.character) + 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..8e1ced1 --- /dev/null +++ b/tests/testthat/setup_helpers.R @@ -0,0 +1,10 @@ +# Produce a more informative output when failing expect_equivalent/identical +# testcases for visual comparison +info_comp <- function(actual, expect){ + if (!requireNamespace("glue", quietly = TRUE)){ + return() + } + out <- glue::glue("Expect: {expect}", + "Actual: {actual}", .sep = "\n") + return(out) +} 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)) }) From 05ba420426c5e8083b779d1594e791e98c4380ca Mon Sep 17 00:00:00 2001 From: Matthew Strasiotto Date: Thu, 1 Oct 2020 16:59:41 +1000 Subject: [PATCH 2/4] Remove spurious addition to .gitignore, remove glue from imports Partially revert "Is able to past list-cols to timevis (#11)" This reverts commit 5a3d079a117c6a083bf3bc65e621a29d4aafbaf8. --- .gitignore | 1 - DESCRIPTION | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index db183f4..09a72cb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ .Rhistory .RData inst/doc -.DS_Store diff --git a/DESCRIPTION b/DESCRIPTION index 36de0bf..6c8ea5c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,8 +29,7 @@ Imports: Suggests: knitr (>= 1.7), testthat (>= 0.9.1), - tibble, - glue + tibble License: MIT + file LICENSE Encoding: UTF-8 LazyData: true From a8c76a8030dda2d97322c4894c9772b07e1e3ec0 Mon Sep 17 00:00:00 2001 From: Matthew Strasiotto Date: Thu, 1 Oct 2020 17:04:47 +1000 Subject: [PATCH 3/4] Remove usage of glue package from unit test helpers --- tests/testthat/setup_helpers.R | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/testthat/setup_helpers.R b/tests/testthat/setup_helpers.R index 8e1ced1..a18e9b3 100644 --- a/tests/testthat/setup_helpers.R +++ b/tests/testthat/setup_helpers.R @@ -1,10 +1,9 @@ # Produce a more informative output when failing expect_equivalent/identical # testcases for visual comparison info_comp <- function(actual, expect){ - if (!requireNamespace("glue", quietly = TRUE)){ - return() - } - out <- glue::glue("Expect: {expect}", - "Actual: {actual}", .sep = "\n") - return(out) + paste0( + c("Expect: ", "Actual: "), + c(expect, actual), + collapse = "\n" + ) } From 410dab38e42b5392459a17004634aa8994bf0614 Mon Sep 17 00:00:00 2001 From: Matthew Strasiotto Date: Thu, 1 Oct 2020 17:12:06 +1000 Subject: [PATCH 4/4] Update changelog --- NEWS.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NEWS.md b/NEWS.md index 4a7e867..d4272a4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # timevis 0.5.* +### Additions + +- List-Columns may now be passed to `timevis`, to support use of `nestedGroups` + ( `nestedGroups` is added in recent versions of `vis-timeline` thanks @matthewstrasiotto ) + TODO - Add `timezone` parameter to `timevis()` - supports showing the timeline in a different timezone than your local machine (#67)