diff --git a/R/append_values.r b/R/append_values.r index 383d0a0..aa6ae38 100644 --- a/R/append_values.r +++ b/R/append_values.r @@ -15,7 +15,7 @@ append_values_factory <- function(type, as.value) { function(x, column.name = type, force=TRUE) { - assert_that(is.tbl_json(x)) + if (!is.tbl_json(x)) x <- as.tbl_json(x) # Extract json json <- attr(x, "JSON") diff --git a/R/data-commits.r b/R/data-commits.r index a9bde7c..0456d6a 100644 --- a/R/data-commits.r +++ b/R/data-commits.r @@ -13,7 +13,6 @@ #' #' # Extract metadata for every commit #' commits %>% # single json document of github commits from dplyr -#' as.tbl_json %>% # turn into a 'tbl_json' #' gather_array %>% # stack as an array #' spread_values( #' sha = jstring("sha"), diff --git a/R/data-companies.r b/R/data-companies.r index 158f280..4da5533 100644 --- a/R/data-companies.r +++ b/R/data-companies.r @@ -15,7 +15,6 @@ #' #' # Get the key employees data #' key_employees <- companies %>% -#' as.tbl_json %>% #' spread_values( #' name = jstring("name") #' ) %>% diff --git a/R/data-issues.r b/R/data-issues.r index f1db9a0..31c3cc4 100644 --- a/R/data-issues.r +++ b/R/data-issues.r @@ -13,7 +13,6 @@ #' #' # Extract metadata for every issue #' issues %>% # single json document of github issues from dplyr -#' as.tbl_json %>% # turn into a 'tbl_json' #' gather_array %>% # stack as an array #' spread_values( #' id = jnumber("id"), @@ -27,7 +26,6 @@ #' #' # Extract label content for issues with labels #' issues %>% # single json document of github issues from dplyr -#' as.tbl_json %>% # turn into a 'tbl_json' #' gather_array %>% # stack as an array #' spread_values(id = jnumber("id")) %>% # capture issue id for relational purposes #' enter_object("labels") %>% # filter just those with labels @@ -39,7 +37,8 @@ #' ) #' #' # Get all URLs at the top level of the JSON -#' issues %>% as.tbl_json %>% gather_array %>% +#' issues %>% +#' gather_array %>% #' gather_keys %>% #' append_values_string() %>% #' filter(grepl("url", key)) diff --git a/R/data-worldbank.r b/R/data-worldbank.r index 6264b33..3f57622 100644 --- a/R/data-worldbank.r +++ b/R/data-worldbank.r @@ -15,7 +15,6 @@ #' #' # Get the top 10 sectors by funded project in Africa #' wb_sectors <- worldbank %>% # 500 Projects funded by the world bank -#' as.tbl_json %>% # turn into a tbl_json object #' spread_values( #' name = jstring("project_name"), # Spread name #' region = jstring("regionname") # Spread region diff --git a/R/enter_object.r b/R/enter_object.r index 23e871f..8afb351 100644 --- a/R/enter_object.r +++ b/R/enter_object.r @@ -5,7 +5,7 @@ #' @export enter_object <- function(x, ...) { - assert_that(is.tbl_json(x)) + if (!is.tbl_json(x)) x <- as.tbl_json(x) # Prepare path path <- prep_path(...) diff --git a/R/gather_array.r b/R/gather_array.r index c5b16f9..28362e9 100644 --- a/R/gather_array.r +++ b/R/gather_array.r @@ -7,7 +7,7 @@ #' @export gather_array <- function(x, column.name = "array.index") { - assert_that(is.tbl_json(x)) + if (!is.tbl_json(x)) x <- as.tbl_json(x) # Get JSON json <- attr(x, "JSON") diff --git a/R/gather_keys.r b/R/gather_keys.r index 77422ab..d41b123 100644 --- a/R/gather_keys.r +++ b/R/gather_keys.r @@ -7,7 +7,7 @@ #' @export gather_keys <- function(x, column.name = "key") { - assert_that(is.tbl_json(x)) + if (!is.tbl_json(x)) x <- as.tbl_json(x) # Get JSON json <- attr(x, "JSON") diff --git a/R/json_lengths.r b/R/json_lengths.r index d86be02..f2fe042 100644 --- a/R/json_lengths.r +++ b/R/json_lengths.r @@ -6,7 +6,7 @@ #' @export json_lengths <- function(x, column.name = "length") { - assert_that(is.tbl_json(x)) + if (!is.tbl_json(x)) x <- as.tbl_json(x) # Extract json json <- attr(x, "JSON") diff --git a/R/json_types.r b/R/json_types.r index 31dab07..9040441 100644 --- a/R/json_types.r +++ b/R/json_types.r @@ -6,7 +6,7 @@ #' @export json_types <- function(x, column.name = "type") { - assert_that(is.tbl_json(x)) + if (!is.tbl_json(x)) x <- as.tbl_json(x) # Extract json json <- attr(x, "JSON") diff --git a/R/spread_values.r b/R/spread_values.r index 2f6f94d..22720fd 100644 --- a/R/spread_values.r +++ b/R/spread_values.r @@ -6,7 +6,7 @@ #' @export spread_values <- function(x, ...) { - assert_that(is.tbl_json(x)) + if (!is.tbl_json(x)) x <- as.tbl_json(x) # Get JSON json <- attr(x, "JSON") diff --git a/man/commits.Rd b/man/commits.Rd index 35bd6c7..fb30bac 100644 --- a/man/commits.Rd +++ b/man/commits.Rd @@ -19,7 +19,6 @@ commits \%>\% substr(1, 2000) \%>\% writeLines # Extract metadata for every commit commits \%>\% # single json document of github commits from dplyr - as.tbl_json \%>\% # turn into a 'tbl_json' gather_array \%>\% # stack as an array spread_values( sha = jstring("sha"), diff --git a/man/companies.Rd b/man/companies.Rd index 93f5354..0559db7 100644 --- a/man/companies.Rd +++ b/man/companies.Rd @@ -19,7 +19,6 @@ companies[[1]] \%>\% writeLines # Get the key employees data key_employees <- companies \%>\% - as.tbl_json \%>\% spread_values( name = jstring("name") ) \%>\% diff --git a/man/issues.Rd b/man/issues.Rd index 9cb9c00..456e8d1 100644 --- a/man/issues.Rd +++ b/man/issues.Rd @@ -19,7 +19,6 @@ issues \%>\% substr(1, 2000) \%>\% writeLines # Extract metadata for every issue issues \%>\% # single json document of github issues from dplyr - as.tbl_json \%>\% # turn into a 'tbl_json' gather_array \%>\% # stack as an array spread_values( id = jnumber("id"), @@ -33,7 +32,6 @@ issues \%>\% # single json document of github issues from dplyr # Extract label content for issues with labels issues \%>\% # single json document of github issues from dplyr - as.tbl_json \%>\% # turn into a 'tbl_json' gather_array \%>\% # stack as an array spread_values(id = jnumber("id")) \%>\% # capture issue id for relational purposes enter_object("labels") \%>\% # filter just those with labels @@ -45,7 +43,8 @@ issues \%>\% # single json document of github issues from dplyr ) # Get all URLs at the top level of the JSON -issues \%>\% as.tbl_json \%>\% gather_array \%>\% +issues \%>\% + gather_array \%>\% gather_keys \%>\% append_values_string() \%>\% filter(grepl("url", key)) diff --git a/man/worldbank.Rd b/man/worldbank.Rd index e150969..9ad5e6d 100644 --- a/man/worldbank.Rd +++ b/man/worldbank.Rd @@ -19,7 +19,6 @@ worldbank[[1]] \%>\% writeLines # Get the top 10 sectors by funded project in Africa wb_sectors <- worldbank \%>\% # 500 Projects funded by the world bank - as.tbl_json \%>\% # turn into a tbl_json object spread_values( name = jstring("project_name"), # Spread name region = jstring("regionname") # Spread region diff --git a/tests/testthat/test-append_values.r b/tests/testthat/test-append_values.r index 346b037..7a6b8c0 100644 --- a/tests/testthat/test-append_values.r +++ b/tests/testthat/test-append_values.r @@ -5,7 +5,7 @@ test_that("has correct complete structure with simple input", { json <- '[{"name": "anne"}, {"name": "bob"}, {"name": "george"}]' expect_identical( - json %>% as.tbl_json %>% gather_array %>% gather_keys %>% + json %>% gather_array %>% gather_keys %>% append_values_string, tbl_json( data.frame( @@ -27,7 +27,7 @@ test_that("string works with value array", { json <- '["a", "b"]' expect_identical( - json %>% as.tbl_json %>% gather_array %>% append_values_string, + json %>% gather_array %>% append_values_string, tbl_json( data.frame( document.id = c(1L, 1L), @@ -47,7 +47,7 @@ test_that("string works with simple input", { json <- '["a", "b", null]' expect_identical( - (json %>% as.tbl_json %>% gather_array %>% append_values_string)$string, + (json %>% gather_array %>% append_values_string)$string, c("a", "b", NA_character_) ) @@ -59,7 +59,7 @@ test_that("number works with simple input", { json <- '[1, 2, null]' expect_identical( - (json %>% as.tbl_json %>% gather_array %>% append_values_number)$number, + (json %>% gather_array %>% append_values_number)$number, c(1, 2, NA_real_) ) @@ -71,7 +71,7 @@ test_that("logical works with simple input", { json <- '[true, false, null]' expect_identical( - (json %>% as.tbl_json %>% gather_array %>% append_values_logical)$logical, + (json %>% gather_array %>% append_values_logical)$logical, c(TRUE, FALSE, NA) ) @@ -80,7 +80,7 @@ test_that("logical works with simple input", { test_that("handles mixed input as appropriate NA", { - data <- '["a", 1, true, null]' %>% as.tbl_json %>% gather_array + data <- '["a", 1, true, null]' %>% gather_array expect_identical( (data %>% append_values_string)$string, c("a", "1", "TRUE", NA_character_) @@ -111,7 +111,7 @@ test_that("correctly handles character(0)", { list()) expect_identical( - character(0) %>% as.tbl_json %>% append_values_string, + character(0) %>% append_values_string, empty) } @@ -130,7 +130,7 @@ test_that("correctly handles {}", { list(nl)) expect_identical( - '{}' %>% as.tbl_json %>% append_values_string, + '{}' %>% append_values_string, empty) } @@ -146,7 +146,7 @@ test_that("correctly handles []", { list(list())) expect_identical( - '[]' %>% as.tbl_json %>% append_values_string, + '[]' %>% append_values_string, empty) } @@ -154,7 +154,7 @@ test_that("correctly handles []", { test_that("correctly handles mixed types when force=FALSE", { - data <- '["a", 1, true, null]' %>% as.tbl_json %>% gather_array + data <- '["a", 1, true, null]' %>% gather_array expect_identical( (data %>% append_values_string(force=FALSE))$string, @@ -173,7 +173,7 @@ test_that("correctly handles mixed types when force=FALSE", { test_that("correctly handles append when trying to append an array", { - data <- '[["a", "b", "c"], "d", "e", "f"]' %>% as.tbl_json %>% gather_array + data <- '[["a", "b", "c"], "d", "e", "f"]' %>% gather_array expect_identical( (data %>% append_values_string())$string, diff --git a/tests/testthat/test-enter_object.r b/tests/testthat/test-enter_object.r index 8fcbc29..1a9e95d 100644 --- a/tests/testthat/test-enter_object.r +++ b/tests/testthat/test-enter_object.r @@ -5,7 +5,7 @@ test_that("filter works with one path", { json <- '{"name": "bob", "attributes": {"age": 32, "gender": "male"}}' expect_identical( - json %>% as.tbl_json %>% enter_object("attributes"), + json %>% enter_object("attributes"), tbl_json( data.frame(document.id = 1L), list(list(age = 32L, gender = "male")) @@ -20,7 +20,7 @@ test_that("filter works with multiple depth paths", { json <- '{"name": "bob", "attributes": { "demographics": {"age": 32, "gender": "male"}}}' expect_identical( - json %>% as.tbl_json %>% enter_object("attributes", "demographics"), + json %>% enter_object("attributes", "demographics"), tbl_json( data.frame(document.id = 1L), list(list(age = 32L, gender = "male")) @@ -38,7 +38,7 @@ test_that("filter removes records with missing path", { ) expect_identical( - json %>% as.tbl_json %>% spread_values(name = jstring("name")) %>% + json %>% spread_values(name = jstring("name")) %>% enter_object("attributes"), tbl_json( data.frame( @@ -57,7 +57,7 @@ test_that("works if no paths exist", { json <- '{"name": "bob"}' expect_identical( - json %>% as.tbl_json %>% spread_values(name = jstring("name")) %>% + json %>% spread_values(name = jstring("name")) %>% enter_object("attributes"), tbl_json( data.frame( @@ -80,15 +80,15 @@ test_that("correctly handles character(0), {}, []", { list()) expect_identical( - character(0) %>% as.tbl_json %>% enter_object("key"), + character(0) %>% enter_object("key"), empty) expect_identical( - '{}' %>% as.tbl_json %>% enter_object("key"), + '{}' %>% enter_object("key"), empty) expect_identical( - '[]' %>% as.tbl_json %>% enter_object("key"), + '[]' %>% enter_object("key"), empty) } diff --git a/tests/testthat/test-gather_array.r b/tests/testthat/test-gather_array.r index 71de9a6..332d8ea 100644 --- a/tests/testthat/test-gather_array.r +++ b/tests/testthat/test-gather_array.r @@ -5,7 +5,7 @@ test_that("works with array of length 1", { json <- '[{"name": "bob"}]' expect_identical( - json %>% as.tbl_json %>% gather_array, + json %>% gather_array, tbl_json( data.frame( document.id = 1L, @@ -25,7 +25,7 @@ test_that("works with single array", { json <- '[{"name": "bob"}, {"name": "susan"}]' expect_identical( - json %>% as.tbl_json %>% gather_array, + json %>% gather_array, tbl_json( data.frame( document.id = c(1L, 1L), @@ -48,7 +48,7 @@ test_that("works with multiple json", { ) expect_identical( - json %>% as.tbl_json %>% gather_array, + json %>% gather_array, tbl_json( data.frame( document.id = c(1L, 1L, 2L), @@ -68,7 +68,7 @@ test_that("works with value array", { json <- c('["a", "b"]') expect_identical( - json %>% as.tbl_json %>% gather_array, + json %>% gather_array, tbl_json( data.frame( document.id = c(1L, 1L), @@ -86,7 +86,7 @@ test_that("empty json are dropped", { json <- c('[{"name": "bob"}]', '[]') expect_identical( - json %>% as.tbl_json %>% gather_array, + json %>% gather_array, tbl_json( data.frame( document.id = 1L, @@ -106,7 +106,7 @@ test_that("null values are kept", { json <- '["string", null]' expect_identical( - json %>% as.tbl_json %>% gather_array, + json %>% gather_array, tbl_json( data.frame( document.id = c(1L, 1L), @@ -123,7 +123,7 @@ test_that("objects throws error", { json <- c('[{"name": "bob"}]', '{"name": "susan"}') - expect_error(json %>% as.tbl_json %>% gather_array) + expect_error(json %>% gather_array) } ) @@ -132,7 +132,7 @@ test_that("values throws error", { json <- c('[{"name": "bob"}]', '"bob"') - expect_error(json %>% as.tbl_json %>% gather_array) + expect_error(json %>% gather_array) } ) @@ -147,13 +147,13 @@ test_that("correctly handles character(0), {}, []", { list()) expect_identical( - character(0) %>% as.tbl_json %>% gather_array, + character(0) %>% gather_array, empty) - expect_error('{}' %>% as.tbl_json %>% gather_array) + expect_error('{}' %>% gather_array) expect_identical( - '[]' %>% as.tbl_json %>% gather_array, + '[]' %>% gather_array, empty) } diff --git a/tests/testthat/test-gather_keys.r b/tests/testthat/test-gather_keys.r index 36e67f2..eedc501 100644 --- a/tests/testthat/test-gather_keys.r +++ b/tests/testthat/test-gather_keys.r @@ -5,7 +5,7 @@ test_that("works in a simple case", { json <- '{"key1": 1, "key2": 2}' expect_identical( - json %>% as.tbl_json %>% gather_keys, + json %>% gather_keys, tbl_json( data.frame( document.id = c(1L, 1L), @@ -29,7 +29,7 @@ test_that("works with compound values", { }' expect_identical( - json %>% as.tbl_json %>% gather_keys, + json %>% gather_keys, tbl_json( data.frame( document.id = c(1L, 1L, 1L, 1L), @@ -45,9 +45,9 @@ test_that("works with compound values", { test_that("throws errors with incorrect types", { - expect_error('1' %>% as.tbl_json %>% gather_keys()) - expect_error('["a"]' %>% as.tbl_json %>% gather_keys()) - expect_error('null' %>% as.tbl_json %>% gather_keys()) + expect_error('1' %>% gather_keys()) + expect_error('["a"]' %>% gather_keys()) + expect_error('null' %>% gather_keys()) } ) @@ -62,15 +62,15 @@ test_that("correctly handles character(0), {}, []", { list()) expect_identical( - character(0) %>% as.tbl_json %>% gather_keys, + character(0) %>% gather_keys, empty) expect_identical( - '{}' %>% as.tbl_json %>% gather_keys, + '{}' %>% gather_keys, empty ) - expect_error('[]' %>% as.tbl_json %>% gather_keys) + expect_error('[]' %>% gather_keys) } ) \ No newline at end of file diff --git a/tests/testthat/test-json_lengths.r b/tests/testthat/test-json_lengths.r index 8deafc1..a1d1f57 100644 --- a/tests/testthat/test-json_lengths.r +++ b/tests/testthat/test-json_lengths.r @@ -4,7 +4,7 @@ test_that("works for arrays", { json <- c('[]', '[1]', '[1, 2]') expect_identical( - json %>% as.tbl_json %>% json_lengths %>% `$`(length), + json %>% json_lengths %>% `$`(length), c(0L, 1L, 2L) ) @@ -15,7 +15,7 @@ test_that("works for objects", { json <- c('{}', '{"k":"v"}', '{"k1":"v1", "k2":"v2"}') expect_identical( - json %>% as.tbl_json %>% json_lengths %>% `$`(length), + json %>% json_lengths %>% `$`(length), c(0L, 1L, 2L) ) @@ -26,7 +26,7 @@ test_that("works for scalars", { json <- c('[1, "a", true]') expect_identical( - json %>% as.tbl_json %>% gather_array %>% json_lengths %>% `$`(length), + json %>% gather_array %>% json_lengths %>% `$`(length), rep(1L, 3) ) @@ -37,13 +37,13 @@ test_that("works for emtpy objects", { json <- character(0) expect_identical( - json %>% as.tbl_json %>% json_lengths %>% `$`(length), + json %>% json_lengths %>% `$`(length), integer(0) ) json <- c('[null, [], {}]') expect_identical( - json %>% as.tbl_json %>% gather_array %>% json_lengths %>% `$`(length), + json %>% gather_array %>% json_lengths %>% `$`(length), rep(0L, 3) ) diff --git a/tests/testthat/test-json_types.r b/tests/testthat/test-json_types.r index 5de52ee..4ecdafc 100644 --- a/tests/testthat/test-json_types.r +++ b/tests/testthat/test-json_types.r @@ -5,7 +5,7 @@ test_that("works with simple input", { json <- '[{"key":"value"}, [1, 2], "string", 1, true, false, null]' expect_identical( - json %>% as.tbl_json %>% gather_array %>% json_types, + json %>% gather_array %>% json_types, tbl_json( data.frame( document.id = rep(1L, 7), @@ -27,7 +27,7 @@ test_that("works with varying array types", { json <- '[[1, 2], [1, null], [{"key":"value"}], [null]]' expect_identical( - (json %>% as.tbl_json %>% gather_array %>% json_types)$type, + (json %>% gather_array %>% json_types)$type, factor(rep("array", 4), levels = allowed_json_types) ) @@ -39,7 +39,7 @@ test_that("works with varying empty data", { json <- '[[], {}, null]' expect_identical( - (json %>% as.tbl_json %>% gather_array %>% json_types)$type, + (json %>% gather_array %>% json_types)$type, factor(c("array", "object", "null"), levels = allowed_json_types) ) @@ -49,7 +49,7 @@ test_that("works with varying empty data", { test_that("works with character(0)", { expect_identical( - (character(0) %>% as.tbl_json %>% json_types)$type, + (character(0) %>% json_types)$type, factor(character(0), levels = allowed_json_types) ) diff --git a/tests/testthat/test-spread_values.r b/tests/testthat/test-spread_values.r index ea1d774..d6f6704 100644 --- a/tests/testthat/test-spread_values.r +++ b/tests/testthat/test-spread_values.r @@ -74,7 +74,7 @@ test_that("exctract various values", { json <- '{"name": "bob", "age": 32, "customer": true}' expect_identical( - json %>% as.tbl_json %>% + json %>% spread_values( name = jstring("name"), age = jnumber("age"), @@ -102,7 +102,7 @@ test_that("exctract down a path", { json <- '{"name": {"first": "bob", "last": "smith"}}' expect_identical( - json %>% as.tbl_json %>% + json %>% spread_values(first.name = jstring("name", "first")), tbl_json( data.frame( @@ -129,7 +129,7 @@ test_that("correctly handles character(0)", { list()) expect_identical( - character(0) %>% as.tbl_json %>% spread_values(value = jstring("key")), + character(0) %>% spread_values(value = jstring("key")), empty) } @@ -147,7 +147,7 @@ test_that("correctly handles {}", { list(nl)) expect_identical( - '{}' %>% as.tbl_json %>% spread_values(value = jstring("key")), + '{}' %>% spread_values(value = jstring("key")), empty ) @@ -166,7 +166,7 @@ test_that("correctly handles []", { list(list())) expect_identical( - '[]' %>% as.tbl_json %>% spread_values(value = jstring("key")), + '[]' %>% spread_values(value = jstring("key")), empty )