Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is able to past list-cols to timevis (#11) #77

Merged
merged 6 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
11 changes: 5 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
}
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/setup_helpers.R
Original file line number Diff line number Diff line change
@@ -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"
)
}
42 changes: 38 additions & 4 deletions tests/testthat/test-dataframeToD3.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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))
})