Skip to content

Commit

Permalink
add colname to data; thx @yonicd for the idea; should fit nicely Sear…
Browse files Browse the repository at this point in the history
…chTree
  • Loading branch information
timelyportfolio committed Dec 2, 2016
1 parent c833683 commit 5787e03
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
9 changes: 7 additions & 2 deletions R/hierarchy.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
change_to_id <- function(x, column=1){
dplyr::mutate(x, children = lapply(
quote(children),
function(y) dplyr::rename_(y,id=colnames(y)[column])
function(y) {
dplyr::mutate(
dplyr::rename_(y,id=colnames(y)[column]),
"colname" = colnames(y)[column]
)
}
))
}

Expand All @@ -33,7 +38,7 @@ promote_na_one <- function(x){
if(length(na_child_loc)){
x <- dplyr::bind_cols(
x,
dplyr::select(na_child,-(match(colnames(na_child),c("id","children"))))
dplyr::select(na_child,-(match(colnames(na_child),c("id","children","colname"))))
)

# eliminate na child
Expand Down
46 changes: 26 additions & 20 deletions tests/testthat/test_hier.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,37 @@ test_that("d3_nest works as json", {
# default as json
expect_identical(
unclass(d3_nest(hier, value_cols="size")),
'{"children":[{"id":"A","children":[{"id":"A.1","children":[{"id":"A.1.a","size":2}],"size":1},{"id":"A.2","children":[{"id":"A.1.b","size":3},{"id":"A.1.c","size":4}]}]},{"id":"B","children":[{"id":"B.1","children":[{"id":"B.1.a","size":5}]},{"id":"B.2","children":[],"size":6},{"id":"B.3","children":[{"id":"B.1.b","size":7}]}]}],"id":"root"}'
'{"children":[{"id":"A","children":[{"id":"A.1","children":[{"id":"A.1.a","size":2,"colname":"lvl3"}],"size":1,"colname":"lvl2"},{"id":"A.2","children":[{"id":"A.1.b","size":3,"colname":"lvl3"},{"id":"A.1.c","size":4,"colname":"lvl3"}],"colname":"lvl2"}],"colname":"lvl1"},{"id":"B","children":[{"id":"B.1","children":[{"id":"B.1.a","size":5,"colname":"lvl3"}],"colname":"lvl2"},{"id":"B.2","children":[],"size":6,"colname":"lvl2"},{"id":"B.3","children":[{"id":"B.1.b","size":7,"colname":"lvl3"}],"colname":"lvl2"}],"colname":"lvl1"}],"id":"root"}'
)

# json = FALSE
expect_identical(
d3_nest(hier, value_cols="size", json=FALSE),
structure(list(children = list(structure(list(id = c("A", "B"
structure(list(children = list(structure(list(id = c("A", "B"
), children = list(structure(list(id = c("A.1", "A.2"), children = list(
structure(list(id = "A.1.a", size = 2), .Names = c("id",
"size"), row.names = c(NA, -1L), class = c("tbl_df", "tbl",
"data.frame")), structure(list(id = c("A.1.b", "A.1.c"),
size = c(3, 4)), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("id", "size"))), size = c(1,
NA)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
), .Names = c("id", "children", "size")), structure(list(id = c("B.1",
"B.2", "B.3"), children = list(structure(list(id = "B.1.a", size = 5), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("id",
"size")), structure(list(id = character(0), size = numeric(0)), .Names = c("id",
"size"), row.names = integer(0), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(id = "B.1.b", size = 7), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("id",
"size"))), size = c(NA, 6, NA)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("id", "children", "size")))), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("id",
"children"))), id = "root"), .Names = c("children", "id"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))
structure(list(id = "A.1.a", size = 2, colname = "lvl3"), .Names = c("id",
"size", "colname"), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(id = c("A.1.b", "A.1.c"
), size = c(3, 4), colname = c("lvl3", "lvl3")), .Names = c("id",
"size", "colname"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L))), size = c(1, NA), colname = c("lvl2",
"lvl2")), .Names = c("id", "children", "size", "colname"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -2L)), structure(list(
id = c("B.1", "B.2", "B.3"), children = list(structure(list(
id = "B.1.a", size = 5, colname = "lvl3"), .Names = c("id",
"size", "colname"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(id = character(0),
size = numeric(0), colname = character(0)), .Names = c("id",
"size", "colname"), row.names = integer(0), class = c("tbl_df",
"tbl", "data.frame")), structure(list(id = "B.1.b", size = 7,
colname = "lvl3"), .Names = c("id", "size", "colname"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-1L))), size = c(NA, 6, NA), colname = c("lvl2", "lvl2",
"lvl2")), .Names = c("id", "children", "size", "colname"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -3L))), colname = c("lvl1",
"lvl1")), .Names = c("id", "children", "colname"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -2L))), id = "root"), .Names = c("children",
"id"), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"
))
)
})

0 comments on commit 5787e03

Please sign in to comment.