Skip to content

Commit

Permalink
Fix spread_all recursive=FALSE
Browse files Browse the repository at this point in the history
Closes #65
Add tests
  • Loading branch information
colearendt committed Jun 14, 2017
1 parent 587734f commit 64e361b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
10 changes: 7 additions & 3 deletions R/spread_all.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,19 @@ spread_all <- function(.x, recursive = TRUE, sep = ".") {
gather_object("..name1") %>%
json_types("..type")

if (recursive)
if (recursive) {
while(any(y$..type == "object"))
y <- rbind_tbl_json(
y %>% dplyr::filter(..type != "object"),
recursive_gather(y, sep)
)
} else {
y <- y %>% dplyr::filter(..type != 'object')
}


# Look for duplicate keys
key_freq <- y %>% group_by(..id, ..name1) %>% tally
key_freq <- y %>% dplyr::group_by(..id, ..name1) %>% dplyr::tally()

if (any(key_freq$n > 1) || any(key_freq$..name1 %in% exist_cols)) {

Expand Down Expand Up @@ -162,7 +166,7 @@ spread_type <- function(.x, this.type, append.fun) {
any_type <- any(.x$..type == this.type)

if (!any_type)
return(data_frame(..id = integer(0)))
return(dplyr::data_frame(..id = integer(0)))

.x %>%
dplyr::filter(..type == this.type) %>%
Expand Down
48 changes: 38 additions & 10 deletions tests/testthat/test-spread_all.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("works for simple example", {
expect_identical(
'{"a": 1, "b": "x", "c": true}' %>% spread_all,
tbl_json(
data_frame(
dplyr::data_frame(
document.id = 1L,
a = 1,
b = "x",
Expand All @@ -22,7 +22,7 @@ test_that("spreads a null column", {
expect_identical(
'{"a": null}' %>% spread_all,
tbl_json(
data_frame(
dplyr::data_frame(
document.id = 1L,
a = NA
),
Expand All @@ -43,15 +43,15 @@ test_that("handles a more complex document", {
expect_identical(
json %>% spread_all,
tbl_json(
data_frame(
dplyr::data_frame(
document.id = 1L:3L,
a = c("x", NA_character_, NA_character_),
b = c(1, NA_integer_, NA_integer_),
c = c(TRUE, NA, NA),
d = rep(NA, 3),
e = rep(NA, 3)
),
json %>% map(fromJSON, simplifyVector = FALSE)
json %>% purrr::map(jsonlite::fromJSON, simplifyVector = FALSE)
)
)

Expand Down Expand Up @@ -102,14 +102,14 @@ test_that("recursive names work", {
expect_identical(
json %>% spread_all,
tbl_json(
data_frame(
dplyr::data_frame(
document.id = 1L,
k1 = 1,
k6 = 4,
k2.k3 = 2,
k2.k4.k5 = 3
),
json %>% map(fromJSON, simplifyVector = FALSE)
json %>% purrr::map(jsonlite::fromJSON, simplifyVector = FALSE)
)
)

Expand Down Expand Up @@ -149,8 +149,8 @@ test_that("works with multiple duplicated columns", {
expect_identical(
suppressWarnings(json %>% spread_all),
tbl_json(
data_frame(document.id = 1L, key = "a", key.2 = "b", key.3 = "c"),
list(fromJSON(json, simplifyVector = FALSE))
dplyr::data_frame(document.id = 1L, key = "a", key.2 = "b", key.3 = "c"),
list(jsonlite::fromJSON(json, simplifyVector = FALSE))
)
)
expect_warning(json %>% spread_all)
Expand All @@ -159,16 +159,44 @@ test_that("works with multiple duplicated columns", {

test_that("works when column names are duplicated from data frame", {

df <- data_frame(key = 1L, json = '{"key": "a", "key": "b"}') %>%
df <- dplyr::data_frame(key = 1L, json = '{"key": "a", "key": "b"}') %>%
as.tbl_json(json.column = "json")

expect_identical(
suppressWarnings(df %>% spread_all),
tbl_json(
data_frame(key = 1L, key.2 = "a", key.3 = "b"),
dplyr::data_frame(key = 1L, key.2 = "a", key.3 = "b"),
attr(df, "JSON")
)
)
expect_warning(df %>% spread_all)

})

test_that("works with recursive=FALSE when objects are present", {
json <- '{"id":1, "name": "Charles", "obj":{"a":2, "b": "test"}}'

j <- json %>% spread_all(recursive=FALSE)

expect_identical(names(j),c('document.id','id','name'))

i <- issues %>% gather_array() %>% spread_all(recursive=FALSE)

expect_equal(nrow(i),30)
expect_equal(ncol(i), 19)
})

test_that("attr(.,JSON) remains intact", {
json <- '{"id": 1, "name": "Charles",
"hobby": ["a","b","c","d"],
"obj": {"a":2, "b": "test"}}'

j <- json %>% spread_all(recursive=FALSE) %>%
spread_values(a=jnumber(obj,a), b=jstring(obj,b)) %>%
enter_object('hobby') %>% gather_array('hobbyid') %>%
append_values_string('hobby')

expect_equal(j$hobby,c('a','b','c','d'))
expect_equal(nrow(j),4)
expect_equal(names(j),c('document.id','id','name','a','b','hobbyid','hobby'))
})

0 comments on commit 64e361b

Please sign in to comment.