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

Nesting Columns, as in nestedGroups is now supported #70

Closed
wants to merge 15 commits into from
Closed
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
inst/doc
.DS_Store
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +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], as.character)
lapply(row[, !is.na(row), drop = FALSE], function(x){
if (lengths(x) > 1 | is.list(x)) return(lapply(unlist(x),as.character))
# if (is.null(x)) return(NA)
return(as.character(x))
})
})
}

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/setup_helpers.R
Original file line number Diff line number Diff line change
@@ -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)
}
34 changes: 30 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,28 @@ 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))
})