From 81b00a494209c809d5020dcf8c91bc9e64a889b3 Mon Sep 17 00:00:00 2001 From: Cole Arendt Date: Sat, 19 Aug 2017 15:31:13 -0400 Subject: [PATCH 1/4] Update badges --- README.Rmd | 5 ++++- README.md | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.Rmd b/README.Rmd index e05cd29..03bd652 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,9 +18,12 @@ knitr::opts_chunk$set( [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/tidyjson)](https://cran.r-project.org/package=tidyjson) [![Build Status](https://travis-ci.org/jeremystan/tidyjson.svg?branch=master)](https://travis-ci.org/jeremystan/tidyjson) -[![Coverage Status](https://img.shields.io/codecov/c/github/jeremystan/tidyjson/master.svg)](https://codecov.io/github/jeremystan/tidyjson?branch=master) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/colearendt/tidyjson?branch=master&svg=true)](https://ci.appveyor.com/project/colearendt/tidyjson) +[![Coverage Status](https://codecov.io/github/jeremystan/tidyjson/coverage.svg?branch=master)](https://codecov.io/github/jeremystan/tidyjson?branch=master) +[![CRAN Activity](http://cranlogs.r-pkg.org/badges/tidyjson)](https://cran.r-project.org/web/packages/tidyjson/index.html) +[![CRAN History](http://cranlogs.r-pkg.org/badges/grand-total/tidyjson)](https://cran.r-project.org/web/packages/tidyjson/index.html) + ![tidyjson graphs](https://cloud.githubusercontent.com/assets/2284427/18217882/1b3b2db4-7114-11e6-8ba3-07938f1db9af.png) tidyjson provides tools for turning complex [json](http://www.json.org/) into [tidy](https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html) diff --git a/README.md b/README.md index 627387c..af3d2ee 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ tidyjson ======== -[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/tidyjson)](https://cran.r-project.org/package=tidyjson) [![Build Status](https://travis-ci.org/jeremystan/tidyjson.svg?branch=master)](https://travis-ci.org/jeremystan/tidyjson) [![Coverage Status](https://img.shields.io/codecov/c/github/jeremystan/tidyjson/master.svg)](https://codecov.io/github/jeremystan/tidyjson?branch=master) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/colearendt/tidyjson?branch=master&svg=true)](https://ci.appveyor.com/project/colearendt/tidyjson) +[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/tidyjson)](https://cran.r-project.org/package=tidyjson) [![Build Status](https://travis-ci.org/jeremystan/tidyjson.svg?branch=master)](https://travis-ci.org/jeremystan/tidyjson) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/colearendt/tidyjson?branch=master&svg=true)](https://ci.appveyor.com/project/colearendt/tidyjson) + +[![Coverage Status](https://codecov.io/github/jeremystan/tidyjson/coverage.svg?branch=master)](https://codecov.io/github/jeremystan/tidyjson?branch=master) [![CRAN Activity](http://cranlogs.r-pkg.org/badges/tidyjson)](https://cran.r-project.org/web/packages/tidyjson/index.html) [![CRAN History](http://cranlogs.r-pkg.org/badges/grand-total/tidyjson)](https://cran.r-project.org/web/packages/tidyjson/index.html) ![tidyjson graphs](https://cloud.githubusercontent.com/assets/2284427/18217882/1b3b2db4-7114-11e6-8ba3-07938f1db9af.png) From 91167bf1f864c9f254d8cafdd87e672028fa6e79 Mon Sep 17 00:00:00 2001 From: Cole Arendt Date: Sat, 19 Aug 2017 15:44:23 -0400 Subject: [PATCH 2/4] Append_values undo rename --- NAMESPACE | 3 -- NEWS.md | 2 -- R/append_values.R | 33 ++++------------- R/enter_object.R | 4 +-- R/gather.R | 10 +++--- R/spread_all.R | 6 ++-- man/append_values.Rd | 21 ++++------- man/enter_object.Rd | 4 +-- man/gather_array.Rd | 4 +-- man/gather_object.Rd | 6 ++-- tests/testthat/test-append_values.R | 56 +++++++++++++---------------- tests/testthat/test-spread_all.R | 2 +- tests/testthat/test-tbl_json.R | 2 +- 13 files changed, 55 insertions(+), 98 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 69bc675..4df2297 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,9 +15,6 @@ S3method(print,tbl_json) S3method(slice,tbl_json) S3method(slice_,tbl_json) export("%>%") -export(append_chr) -export(append_dbl) -export(append_lgl) export(append_values_logical) export(append_values_number) export(append_values_string) diff --git a/NEWS.md b/NEWS.md index 0c54106..b9c073d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -31,8 +31,6 @@ the missing `document.id`. (#86) * `is_json_string()`,`is_json_number()`,`is_json_logical()` -> use `is_json_chr()`, `is_json_dbl()`, `is_json_lgl()` instead (#93) -* `append_values_string()`, `append_values_number()`, `append_values_logical()` -> use `append_chr()`, `append_dbl()`, `append_lgl()` instead (#93) - # tidyjson 0.2.1.9000 ## New functions diff --git a/R/append_values.R b/R/append_values.R index 99ac05e..bdd7dcd 100644 --- a/R/append_values.R +++ b/R/append_values.R @@ -7,7 +7,7 @@ #' #' Any values that can not be converted to the specified will be \code{NA} in #' the resulting column. This includes other scalar types (e.g., numbers or -#' logicals if you are using \code{append_chr}) and *also* any rows +#' logicals if you are using \code{append_values_string}) and *also* any rows #' where the JSON is NULL or an object or array. #' #' Note that the \code{append_values} functions do not alter the JSON @@ -30,7 +30,7 @@ #' # Stack names #' '{"first": "bob", "last": "jones"}' %>% #' gather_object %>% -#' append_chr +#' append_values_string #' #' # This is most useful when data is stored in name-value pairs #' # For example, tags in recipes: @@ -40,7 +40,7 @@ #' spread_values(name = json_chr(name)) %>% #' enter_object(tags) %>% #' gather_object("tag") %>% -#' append_dbl("count") +#' append_values_number("count") NULL #' Creates the append_values_* functions @@ -120,33 +120,12 @@ append_values_type <- function(json, type) { #' @export #' @rdname append_values -append_chr <- append_values_factory("string", as.character) +append_values_string <- append_values_factory("string", as.character) #' @export #' @rdname append_values -append_values_string <- function(.x, column.name = 'string', force = TRUE, recursive = FALSE){ - .Deprecated(new='append_chr') - append_chr(.x,column.name,force,recursive) -} - -#' @export -#' @rdname append_values -append_dbl <- append_values_factory("number", as.numeric) - -#' @export -#' @rdname append_values -append_values_number <- function(.x, column.name = 'number', force = TRUE, recursive = FALSE){ - .Deprecated(new='append_dbl') - append_dbl(.x,column.name,force,recursive) -} - -#' @export -#' @rdname append_values -append_lgl <- append_values_factory("logical", as.logical) +append_values_number <- append_values_factory("number", as.numeric) #' @export #' @rdname append_values -append_values_logical <- function(.x, column.name = 'logical', force = TRUE, recursive = FALSE){ - .Deprecated(new='append_lgl') - append_lgl(.x,column.name,force,recursive) -} \ No newline at end of file +append_values_logical <- append_values_factory("logical", as.logical) diff --git a/R/enter_object.R b/R/enter_object.R index 9730829..10ea55f 100644 --- a/R/enter_object.R +++ b/R/enter_object.R @@ -44,10 +44,10 @@ #' json %>% spread_all %>% enter_object(children) %>% #' gather_array("child.num") #' -#' # And append_chr to add the children names +#' # And append_values_string to add the children names #' json %>% spread_all %>% enter_object(children) %>% #' gather_array("child.num") %>% -#' append_chr("child") +#' append_values_string("child") #' #' # The path can be comma delimited to go deep into a nested object #' json <- '{"name": "bob", "attributes": {"age": 32, "gender": "male"}}' diff --git a/R/gather.R b/R/gather.R index f53f646..22ec8cd 100644 --- a/R/gather.R +++ b/R/gather.R @@ -107,14 +107,14 @@ gather_factory <- function(default.column.name, default.column.empty, #' # Then we can use the column.name argument to change the column name #' json %>% gather_object("year") #' -#' # We can also use append_dbl to capture the values, since they are +#' # We can also use append_values_number to capture the values, since they are #' # all of the same type -#' json %>% gather_object("year") %>% append_dbl("count") +#' json %>% gather_object("year") %>% append_values_number("count") #' #' # This can even work with a more complex, nested example #' json <- '{"2015": {"1": 10, "3": 1, "11": 5}, "2016": {"2": 3, "5": 15}}' #' json %>% gather_object("year") %>% gather_object("month") %>% -#' append_dbl("count") +#' append_values_number("count") #' #' # Most JSON starts out as an object (or an array of objects), and #' # gather_object can be used to inspect the top level (or 2nd level) objects @@ -173,7 +173,7 @@ gather_keys <- function(.x, column.name = "key") { #' json %>% gather_array %>% json_types #' #' # Extract string values -#' json %>% gather_array %>% append_chr +#' json %>% gather_array %>% append_values_string #' #' # A more complex mixed type example #' json <- '["a", 1, true, null, {"name": "value"}]' @@ -186,7 +186,7 @@ gather_keys <- function(.x, column.name = "key") { #' #' # Extract both levels #' json %>% gather_array("index.1") %>% gather_array("index.2") %>% -#' append_chr +#' append_values_string #' #' # Some JSON begins as an array #' commits %>% gather_array diff --git a/R/spread_all.R b/R/spread_all.R index 6bf02e3..c0f5776 100644 --- a/R/spread_all.R +++ b/R/spread_all.R @@ -120,9 +120,9 @@ spread_all <- function(.x, recursive = TRUE, sep = ".") { magrittr::extract2("..name1") %>% unique - y_string <- spread_type(y, "string", append_chr) - y_number <- spread_type(y, "number", append_dbl) - y_logical <- spread_type(y, "logical", append_lgl) + y_string <- spread_type(y, "string", append_values_string) + y_number <- spread_type(y, "number", append_values_number) + y_logical <- spread_type(y, "logical", append_values_logical) ## Build data_frame component z <- dplyr::as_tibble(.x) %>% diff --git a/man/append_values.Rd b/man/append_values.Rd index 5b40155..db3268f 100644 --- a/man/append_values.Rd +++ b/man/append_values.Rd @@ -2,27 +2,18 @@ % Please edit documentation in R/append_values.R \name{append_values} \alias{append_values} -\alias{append_chr} \alias{append_values_string} -\alias{append_dbl} \alias{append_values_number} -\alias{append_lgl} \alias{append_values_logical} \title{Appends all JSON values with a specified type as a new column} \usage{ -append_chr(.x, column.name = type, force = TRUE, recursive = FALSE) - -append_values_string(.x, column.name = "string", force = TRUE, +append_values_string(.x, column.name = type, force = TRUE, recursive = FALSE) -append_dbl(.x, column.name = type, force = TRUE, recursive = FALSE) - -append_values_number(.x, column.name = "number", force = TRUE, +append_values_number(.x, column.name = type, force = TRUE, recursive = FALSE) -append_lgl(.x, column.name = type, force = TRUE, recursive = FALSE) - -append_values_logical(.x, column.name = "logical", force = TRUE, +append_values_logical(.x, column.name = type, force = TRUE, recursive = FALSE) } \arguments{ @@ -50,7 +41,7 @@ column named \code{column.name}. This is particularly useful after using \details{ Any values that can not be converted to the specified will be \code{NA} in the resulting column. This includes other scalar types (e.g., numbers or -logicals if you are using \code{append_chr}) and *also* any rows +logicals if you are using \code{append_values_string}) and *also* any rows where the JSON is NULL or an object or array. Note that the \code{append_values} functions do not alter the JSON @@ -61,7 +52,7 @@ attribute of the \code{tbl_json} object in any way. # Stack names '{"first": "bob", "last": "jones"}' \%>\% gather_object \%>\% - append_chr + append_values_string # This is most useful when data is stored in name-value pairs # For example, tags in recipes: @@ -71,7 +62,7 @@ recipes \%>\% spread_values(name = json_chr(name)) \%>\% enter_object(tags) \%>\% gather_object("tag") \%>\% - append_dbl("count") + append_values_number("count") } \seealso{ \code{\link{gather_object}} to gather an object first, diff --git a/man/enter_object.Rd b/man/enter_object.Rd index 9510375..a67535d 100644 --- a/man/enter_object.Rd +++ b/man/enter_object.Rd @@ -53,10 +53,10 @@ json \%>\% spread_all \%>\% enter_object("children") json \%>\% spread_all \%>\% enter_object(children) \%>\% gather_array("child.num") -# And append_chr to add the children names +# And append_values_string to add the children names json \%>\% spread_all \%>\% enter_object(children) \%>\% gather_array("child.num") \%>\% - append_chr("child") + append_values_string("child") # The path can be comma delimited to go deep into a nested object json <- '{"name": "bob", "attributes": {"age": 32, "gender": "male"}}' diff --git a/man/gather_array.Rd b/man/gather_array.Rd index fc0c669..46d203f 100644 --- a/man/gather_array.Rd +++ b/man/gather_array.Rd @@ -48,7 +48,7 @@ json \%>\% json_types json \%>\% gather_array \%>\% json_types # Extract string values -json \%>\% gather_array \%>\% append_chr +json \%>\% gather_array \%>\% append_values_string # A more complex mixed type example json <- '["a", 1, true, null, {"name": "value"}]' @@ -61,7 +61,7 @@ json <- '[["a", "b", "c"], ["a", "d"], ["b", "c"]]' # Extract both levels json \%>\% gather_array("index.1") \%>\% gather_array("index.2") \%>\% - append_chr + append_values_string # Some JSON begins as an array commits \%>\% gather_array diff --git a/man/gather_object.Rd b/man/gather_object.Rd index 171fd2d..c24ec58 100644 --- a/man/gather_object.Rd +++ b/man/gather_object.Rd @@ -47,14 +47,14 @@ json <- '{"2014": 32, "2015": 56, "2016": 14}' # Then we can use the column.name argument to change the column name json \%>\% gather_object("year") -# We can also use append_dbl to capture the values, since they are +# We can also use append_values_number to capture the values, since they are # all of the same type -json \%>\% gather_object("year") \%>\% append_dbl("count") +json \%>\% gather_object("year") \%>\% append_values_number("count") # This can even work with a more complex, nested example json <- '{"2015": {"1": 10, "3": 1, "11": 5}, "2016": {"2": 3, "5": 15}}' json \%>\% gather_object("year") \%>\% gather_object("month") \%>\% - append_dbl("count") + append_values_number("count") # Most JSON starts out as an object (or an array of objects), and # gather_object can be used to inspect the top level (or 2nd level) objects diff --git a/tests/testthat/test-append_values.R b/tests/testthat/test-append_values.R index c36d2c8..1591213 100644 --- a/tests/testthat/test-append_values.R +++ b/tests/testthat/test-append_values.R @@ -6,7 +6,7 @@ test_that("has correct complete structure with simple input", { expect_identical( json %>% gather_array %>% gather_object %>% - append_chr, + append_values_string, tbl_json( data.frame( document.id = c(1L, 1L, 1L), @@ -27,7 +27,7 @@ test_that("string works with value array", { json <- '["a", "b"]' expect_identical( - json %>% gather_array %>% append_chr, + 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 %>% gather_array %>% append_chr)$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 %>% gather_array %>% append_dbl)$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 %>% gather_array %>% append_lgl)$logical, + (json %>% gather_array %>% append_values_logical)$logical, c(TRUE, FALSE, NA) ) @@ -82,18 +82,18 @@ test_that("handles mixed input as appropriate NA", { data <- '["a", 1, true, null]' %>% gather_array expect_identical( - (data %>% append_chr)$string, + (data %>% append_values_string)$string, c("a", "1", "TRUE", NA_character_) ) - expect_warning(tmp_data <- data %>% append_dbl) + expect_warning(tmp_data <- data %>% append_values_number) expect_identical( tmp_data$number, c(NA_real_, 1, NA_real_, NA_real_) ) expect_identical( - (data %>% append_lgl)$logical, + (data %>% append_values_logical)$logical, c(NA, NA, TRUE, NA) ) @@ -111,7 +111,7 @@ test_that("correctly handles character(0)", { list()) expect_identical( - character(0) %>% append_chr, + character(0) %>% append_values_string, empty) } @@ -130,7 +130,7 @@ test_that("correctly handles {}", { list(nl)) expect_identical( - '{}' %>% append_chr, + '{}' %>% append_values_string, empty) } @@ -146,7 +146,7 @@ test_that("correctly handles []", { list(list())) expect_identical( - '[]' %>% append_chr, + '[]' %>% append_values_string, empty) } @@ -157,15 +157,15 @@ test_that("correctly handles mixed types when force=FALSE", { data <- '["a", 1, true, null]' %>% gather_array expect_identical( - (data %>% append_chr(force=FALSE))$string, + (data %>% append_values_string(force=FALSE))$string, c("a", rep(NA_character_,3)) ) expect_identical( - (data %>% append_dbl(force=FALSE))$number, + (data %>% append_values_number(force=FALSE))$number, c(NA_real_, 1, NA_real_, NA_real_) ) expect_identical( - (data %>% append_lgl(force=FALSE))$logical, + (data %>% append_values_logical(force=FALSE))$logical, c(NA, NA, TRUE, NA) ) } @@ -176,7 +176,7 @@ test_that("correctly handles append when trying to append an array", { data <- '[["a", "b", "c"], "d", "e", "f"]' %>% gather_array expect_identical( - (data %>% append_chr())$string, + (data %>% append_values_string())$string, c(NA_character_, "d", "e", "f") ) } @@ -189,48 +189,40 @@ test_that("recursive works as expected", { expected_val <- c(30, 40, 30) expect_identical( - (data %>% append_dbl(force=TRUE, recursive=FALSE))$number, + (data %>% append_values_number(force=TRUE, recursive=FALSE))$number, expected_na) expect_identical( - (data %>% append_dbl(force=TRUE, recursive=TRUE))$number, + (data %>% append_values_number(force=TRUE, recursive=TRUE))$number, expected_val) expect_identical( - (data %>% append_dbl(force=FALSE, recursive=FALSE))$number, + (data %>% append_values_number(force=FALSE, recursive=FALSE))$number, expected_na) expect_error( - (data %>% append_dbl(force=FALSE, recursive=TRUE))$number) + (data %>% append_values_number(force=FALSE, recursive=TRUE))$number) data <- '{"item1": {"price" : {"usd" : {"real" : 30}}}, "item2" : 40, "item3" : 30}' %>% gather_object expect_identical( - (data %>% append_dbl(recursive=FALSE))$number, + (data %>% append_values_number(recursive=FALSE))$number, expected_na) expect_identical( - (data %>% append_dbl(recursive=TRUE))$number, + (data %>% append_values_number(recursive=TRUE))$number, expected_val) data <- '{"item1": {"price" : 30, "qty" : 1}, "item2" : 40, "item3" : 30}' %>% gather_object expect_identical( - (data %>% append_dbl(recursive=FALSE))$number, + (data %>% append_values_number(recursive=FALSE))$number, expected_na) expect_identical( - (data %>% append_dbl(recursive=TRUE))$number, + (data %>% append_values_number(recursive=TRUE))$number, expected_na) } ) -test_that('deprecated functions warn appropriately', { - deptxt <- function(func,alt) { - paste0(func,'.*deprecated.*',alt,'.*instead') - } - - expect_warning(append_values_string('"a"'),deptxt('append_values_string','append_chr')) - expect_warning(append_values_number('2'),deptxt('append_values_number','append_dbl')) - expect_warning(append_values_logical('true'),deptxt('append_values_logical','append_lgl')) -}) + context("my_unlist") test_that("my_unlist safely handles edge cases", { diff --git a/tests/testthat/test-spread_all.R b/tests/testthat/test-spread_all.R index 1c5be13..afb9198 100644 --- a/tests/testthat/test-spread_all.R +++ b/tests/testthat/test-spread_all.R @@ -194,7 +194,7 @@ test_that("attr(.,JSON) remains intact", { j <- json %>% spread_all(recursive=FALSE) %>% spread_values(a=json_dbl(obj,a), b=json_chr(obj,b)) %>% enter_object('hobby') %>% gather_array('hobbyid') %>% - append_chr('hobby') + append_values_string('hobby') expect_equal(j$hobby,c('a','b','c','d')) expect_equal(nrow(j),4) diff --git a/tests/testthat/test-tbl_json.R b/tests/testthat/test-tbl_json.R index ace2b79..86f48e0 100644 --- a/tests/testthat/test-tbl_json.R +++ b/tests/testthat/test-tbl_json.R @@ -223,7 +223,7 @@ test_that('does not throw an error', { expect_output(json %>% as.tbl_json() %>% print, printregex) j <- json %>% spread_all() %>% enter_object('c') %>% - gather_array('c_id') %>% append_dbl() + gather_array('c_id') %>% append_values_number() expect_output(j %>% print, printregex) From e5ed7c73622dda6f233222adb89ec9d1acb08980 Mon Sep 17 00:00:00 2001 From: Cole Arendt Date: Sat, 19 Aug 2017 16:14:00 -0400 Subject: [PATCH 3/4] Undo is_json and json_ renames - Undo #93 --- NAMESPACE | 6 -- NEWS.md | 6 -- R/append_values.R | 2 +- R/data-issues.R | 2 +- R/data-worldbank.R | 2 +- R/is_json.R | 29 ++------- R/spread_values.R | 54 ++++++----------- R/tbl_json.R | 2 +- man/append_values.Rd | 2 +- man/bind_rows.Rd | 2 +- man/is_json.Rd | 11 +--- man/issues.Rd | 2 +- man/json_factory.Rd | 4 +- man/json_functions.Rd | 9 --- man/spread_values.Rd | 26 ++++---- man/worldbank.Rd | 2 +- tests/testthat/test-enter_object.R | 4 +- tests/testthat/test-is_json.R | 16 +---- tests/testthat/test-spread_all.R | 2 +- tests/testthat/test-spread_values.R | 94 +++++++++++++---------------- tests/testthat/test-tbl_json.R | 48 +++++++-------- vignettes/multiple-apis.Rmd | 54 ++++++++--------- vignettes/visualizing-json.Rmd | 14 ++--- 23 files changed, 151 insertions(+), 242 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 4df2297..1ed567a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,9 +28,6 @@ export(gather_keys) export(gather_object) export(is.tbl_json) export(is_json_array) -export(is_json_chr) -export(is_json_dbl) -export(is_json_lgl) export(is_json_logical) export(is_json_null) export(is_json_number) @@ -39,11 +36,8 @@ export(is_json_scalar) export(is_json_string) export(jlogical) export(jnumber) -export(json_chr) export(json_complexity) -export(json_dbl) export(json_lengths) -export(json_lgl) export(json_schema) export(json_structure) export(json_types) diff --git a/NEWS.md b/NEWS.md index b9c073d..46fa6e9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,12 +25,6 @@ * Fix json_structure() failure if `document.id` missing by imputing the missing `document.id`. (#86) -## Deprecated functions - -* `jstring()`, `jnumber()`, `jlogical()` -> use `json_chr()`, `json_dbl()`, `json_lgl()` instead (#93) - -* `is_json_string()`,`is_json_number()`,`is_json_logical()` -> use `is_json_chr()`, `is_json_dbl()`, `is_json_lgl()` instead (#93) - # tidyjson 0.2.1.9000 ## New functions diff --git a/R/append_values.R b/R/append_values.R index bdd7dcd..ae6289a 100644 --- a/R/append_values.R +++ b/R/append_values.R @@ -37,7 +37,7 @@ #' recipes <- c('{"name": "pie", "tags": {"apple": 10, "pie": 2, "flour": 5}}', #' '{"name": "cookie", "tags": {"chocolate": 2, "cookie": 1}}') #' recipes %>% -#' spread_values(name = json_chr(name)) %>% +#' spread_values(name = jstring(name)) %>% #' enter_object(tags) %>% #' gather_object("tag") %>% #' append_values_number("count") diff --git a/R/data-issues.R b/R/data-issues.R index d397045..bfa267c 100644 --- a/R/data-issues.R +++ b/R/data-issues.R @@ -28,7 +28,7 @@ #' # Count issues labels by name #' labels <- issues %>% #' gather_array %>% # stack issues as "issue.num" -#' spread_values(id = json_dbl(id)) %>% # capture just issue id +#' spread_values(id = jnumber(id)) %>% # capture just issue id #' enter_object(labels) %>% # filter just those with labels #' gather_array("label.index") %>% # stack labels #' spread_all diff --git a/R/data-worldbank.R b/R/data-worldbank.R index 40f7ea2..dc89535 100644 --- a/R/data-worldbank.R +++ b/R/data-worldbank.R @@ -25,7 +25,7 @@ #' select(project_name, regionname) %>% #' enter_object(majorsector_percent) %>% # Enter the 'sector' object #' gather_array("sector.index") %>% # Gather the array -#' spread_values(sector = json_chr(Name)) # Spread the sector name +#' spread_values(sector = jstring(Name)) # Spread the sector name #' #' # Examine the structured data #' wb_sectors %>% glimpse diff --git a/R/is_json.R b/R/is_json.R index dc50f22..914ef19 100644 --- a/R/is_json.R +++ b/R/is_json.R @@ -25,7 +25,7 @@ is_json_factory <- function(desired.types) { #' #' # Test a simple example #' json <- '[1, "string", true, [1, 2], {"name": "value"}, null]' %>% gather_array -#' json %>% is_json_dbl +#' json %>% is_json_number #' json %>% is_json_array #' json %>% is_json_scalar #' @@ -46,36 +46,15 @@ NULL #' @rdname is_json #' @export -is_json_chr <- is_json_factory('string') +is_json_string <- is_json_factory('string') #' @rdname is_json #' @export -is_json_string <- function(.x) { - .Deprecated('is_json_chr') - is_json_chr(.x) -} - -#' @rdname is_json -#' @export -is_json_dbl <- is_json_factory('number') - -#' @rdname is_json -#' @export -is_json_number <- function(.x) { - .Deprecated('is_json_dbl') - is_json_dbl(.x) -} - -#' @rdname is_json -#' @export -is_json_lgl <- is_json_factory('logical') +is_json_number <- is_json_factory('number') #' @rdname is_json #' @export -is_json_logical <- function(.x) { - .Deprecated('is_json_lgl') - is_json_lgl(.x) -} +is_json_logical <- is_json_factory('logical') #' @rdname is_json #' @export diff --git a/R/spread_values.R b/R/spread_values.R index 14f5a54..e451406 100644 --- a/R/spread_values.R +++ b/R/spread_values.R @@ -2,13 +2,13 @@ #' #' The \code{spread_values} function lets you extract extract specific values #' from (potentiall nested) JSON objects. \code{spread_values} takes -#' \code{\link{json_chr}}, \code{\link{json_dbl}} or \code{\link{json_lgl}} named +#' \code{\link{jstring}}, \code{\link{jnumber}} or \code{\link{jlogical}} named #' function calls as arguments in order to specify the type of the data that #' should be captured at each desired name-value pair location. These values can #' be of varying types at varying depths. #' -#' Note that \code{\link{json_chr}}, \code{\link{json_dbl}} and -#' \code{\link{json_lgl}} will fail if they encounter the incorrect type in any +#' Note that \code{\link{jstring}}, \code{\link{jnumber}} and +#' \code{\link{jlogical}} will fail if they encounter the incorrect type in any #' document. #' #' The advantage of \code{spread_values} over \code{\link{spread_all}} is that @@ -19,13 +19,13 @@ #' #' @seealso \code{\link{spread_all}} for spreading all values, #' \code{\link[tidyr]{spread}} for spreading data frames, -#' \code{\link{json_chr}}, \code{\link{json_dbl}}, -#' \code{\link{json_lgl}} for accessing specific names +#' \code{\link{jstring}}, \code{\link{jnumber}}, +#' \code{\link{jlogical}} for accessing specific names #' @param .x a json string or \code{\link{tbl_json}} object #' @param ... \code{column = value} pairs where \code{column} will be the #' column name created and \code{value} must be a call to -#' \code{\link{json_chr}}, \code{\link{json_dbl}} or -#' \code{\link{json_lgl}} specifying the path to get the value (and +#' \code{\link{jstring}}, \code{\link{jnumber}} or +#' \code{\link{jlogical}} specifying the path to get the value (and #' the type implicit in the function name) #' @return a \code{\link{tbl_json}} object #' @export @@ -37,9 +37,9 @@ #' # Using spread_values #' json %>% #' spread_values( -#' first.name = json_chr(name, first), -#' last.name = json_chr(name, last), -#' age = json_dbl(age) +#' first.name = jstring(name, first), +#' last.name = jstring(name, last), +#' age = jnumber(age) #' ) #' #' # Another document, this time with a middle name (and no age) @@ -48,9 +48,9 @@ #' # spread_values still gives the same column structure #' c(json, json2) %>% #' spread_values( -#' first.name = json_chr(name, first), -#' last.name = json_chr(name, last), -#' age = json_dbl(age) +#' first.name = jstring(name, first), +#' last.name = jstring(name, last), +#' age = jnumber(age) #' ) #' #' # whereas spread_all adds a new column @@ -73,7 +73,7 @@ spread_values <- function(.x, ...) { } -#' Factory that creates the json_* functions below +#' Factory that creates the j* functions below #' #' @param map.function function to map to collapse json_factory <- function(map.function) { @@ -119,32 +119,12 @@ NULL #' @rdname json_functions #' @export -json_chr <- json_factory(map_chr) +jstring <- json_factory(map_chr) #' @rdname json_functions #' @export -jstring <- function(..., recursive=FALSE) { - .Deprecated('json_chr') - json_chr(...) -} -#' @rdname json_functions -#' @export -json_dbl <- json_factory(map_dbl) +jnumber <- json_factory(map_dbl) #' @rdname json_functions #' @export -jnumber <- function(..., recursive=FALSE) { - .Deprecated('json_dbl') - json_dbl(...) -} - -#' @rdname json_functions -#' @export -json_lgl <- json_factory(map_lgl) - -#' @rdname json_functions -#' @export -jlogical <- function(..., recursive=FALSE) { - .Deprecated('json_lgl') - json_lgl(...) -} +jlogical <- json_factory(map_lgl) diff --git a/R/tbl_json.R b/R/tbl_json.R index d27b15f..26ed4c3 100644 --- a/R/tbl_json.R +++ b/R/tbl_json.R @@ -229,7 +229,7 @@ slice.tbl_json <- wrap_dplyr_verb(dplyr::slice) #' a <- as.tbl_json('{"a": 1, "b": 2}') #' b <- as.tbl_json('{"a": 3, "b": 4}') #' -#' bind_rows(a,b) %>% spread_values(a=json_dbl(a),b=json_dbl(b)) +#' bind_rows(a,b) %>% spread_values(a=jnumber(a),b=jnumber(b)) #' #' ## as a list #' bind_rows(list(a,b)) %>% spread_all() diff --git a/man/append_values.Rd b/man/append_values.Rd index db3268f..5066f6c 100644 --- a/man/append_values.Rd +++ b/man/append_values.Rd @@ -59,7 +59,7 @@ attribute of the \code{tbl_json} object in any way. recipes <- c('{"name": "pie", "tags": {"apple": 10, "pie": 2, "flour": 5}}', '{"name": "cookie", "tags": {"chocolate": 2, "cookie": 1}}') recipes \%>\% - spread_values(name = json_chr(name)) \%>\% + spread_values(name = jstring(name)) \%>\% enter_object(tags) \%>\% gather_object("tag") \%>\% append_values_number("count") diff --git a/man/bind_rows.Rd b/man/bind_rows.Rd index b502eac..1bcb83d 100644 --- a/man/bind_rows.Rd +++ b/man/bind_rows.Rd @@ -25,7 +25,7 @@ is meant to mask dplyr::bind_rows (although it is called directly). a <- as.tbl_json('{"a": 1, "b": 2}') b <- as.tbl_json('{"a": 3, "b": 4}') -bind_rows(a,b) \%>\% spread_values(a=json_dbl(a),b=json_dbl(b)) +bind_rows(a,b) \%>\% spread_values(a=jnumber(a),b=jnumber(b)) ## as a list bind_rows(list(a,b)) \%>\% spread_all() diff --git a/man/is_json.Rd b/man/is_json.Rd index f179d5c..00583fd 100644 --- a/man/is_json.Rd +++ b/man/is_json.Rd @@ -2,11 +2,8 @@ % Please edit documentation in R/is_json.R \name{is_json} \alias{is_json} -\alias{is_json_chr} \alias{is_json_string} -\alias{is_json_dbl} \alias{is_json_number} -\alias{is_json_lgl} \alias{is_json_logical} \alias{is_json_null} \alias{is_json_array} @@ -14,16 +11,10 @@ \alias{is_json_scalar} \title{Predicates to test for specific JSON types in \code{\link{tbl_json}} objects} \usage{ -is_json_chr(.x) - is_json_string(.x) -is_json_dbl(.x) - is_json_number(.x) -is_json_lgl(.x) - is_json_logical(.x) is_json_null(.x) @@ -49,7 +40,7 @@ filter complex JSON by type before applying \code{\link{gather_object}} or # Test a simple example json <- '[1, "string", true, [1, 2], {"name": "value"}, null]' \%>\% gather_array -json \%>\% is_json_dbl +json \%>\% is_json_number json \%>\% is_json_array json \%>\% is_json_scalar diff --git a/man/issues.Rd b/man/issues.Rd index a9a848c..2cf10e8 100644 --- a/man/issues.Rd +++ b/man/issues.Rd @@ -35,7 +35,7 @@ issues \%>\% gather_array \%>\% gather_object \%>\% json_types \%>\% # Count issues labels by name labels <- issues \%>\% gather_array \%>\% # stack issues as "issue.num" - spread_values(id = json_dbl(id)) \%>\% # capture just issue id + spread_values(id = jnumber(id)) \%>\% # capture just issue id enter_object(labels) \%>\% # filter just those with labels gather_array("label.index") \%>\% # stack labels spread_all diff --git a/man/json_factory.Rd b/man/json_factory.Rd index 4874f8d..65e228e 100644 --- a/man/json_factory.Rd +++ b/man/json_factory.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/spread_values.R \name{json_factory} \alias{json_factory} -\title{Factory that creates the json_* functions below} +\title{Factory that creates the j* functions below} \usage{ json_factory(map.function) } @@ -10,5 +10,5 @@ json_factory(map.function) \item{map.function}{function to map to collapse} } \description{ -Factory that creates the json_* functions below +Factory that creates the j* functions below } diff --git a/man/json_functions.Rd b/man/json_functions.Rd index a096f97..d25ede6 100644 --- a/man/json_functions.Rd +++ b/man/json_functions.Rd @@ -2,25 +2,16 @@ % Please edit documentation in R/spread_values.R \name{json_functions} \alias{json_functions} -\alias{json_chr} \alias{jstring} -\alias{json_dbl} \alias{jnumber} -\alias{json_lgl} \alias{jlogical} \title{Navigates nested objects to get at names of a specific type, to be used as arguments to \code{\link{spread_values}}} \usage{ -json_chr(..., recursive = FALSE) - jstring(..., recursive = FALSE) -json_dbl(..., recursive = FALSE) - jnumber(..., recursive = FALSE) -json_lgl(..., recursive = FALSE) - jlogical(..., recursive = FALSE) } \arguments{ diff --git a/man/spread_values.Rd b/man/spread_values.Rd index 2cbf911..ba0be96 100644 --- a/man/spread_values.Rd +++ b/man/spread_values.Rd @@ -11,8 +11,8 @@ spread_values(.x, ...) \item{...}{\code{column = value} pairs where \code{column} will be the column name created and \code{value} must be a call to -\code{\link{json_chr}}, \code{\link{json_dbl}} or -\code{\link{json_lgl}} specifying the path to get the value (and +\code{\link{jstring}}, \code{\link{jnumber}} or +\code{\link{jlogical}} specifying the path to get the value (and the type implicit in the function name)} } \value{ @@ -21,14 +21,14 @@ a \code{\link{tbl_json}} object \description{ The \code{spread_values} function lets you extract extract specific values from (potentiall nested) JSON objects. \code{spread_values} takes -\code{\link{json_chr}}, \code{\link{json_dbl}} or \code{\link{json_lgl}} named +\code{\link{jstring}}, \code{\link{jnumber}} or \code{\link{jlogical}} named function calls as arguments in order to specify the type of the data that should be captured at each desired name-value pair location. These values can be of varying types at varying depths. } \details{ -Note that \code{\link{json_chr}}, \code{\link{json_dbl}} and -\code{\link{json_lgl}} will fail if they encounter the incorrect type in any +Note that \code{\link{jstring}}, \code{\link{jnumber}} and +\code{\link{jlogical}} will fail if they encounter the incorrect type in any document. The advantage of \code{spread_values} over \code{\link{spread_all}} is that @@ -45,9 +45,9 @@ json <- '{"name": {"first": "Bob", "last": "Jones"}, "age": 32}' # Using spread_values json \%>\% spread_values( - first.name = json_chr(name, first), - last.name = json_chr(name, last), - age = json_dbl(age) + first.name = jstring(name, first), + last.name = jstring(name, last), + age = jnumber(age) ) # Another document, this time with a middle name (and no age) @@ -56,9 +56,9 @@ json2 <- '{"name": {"first": "Ann", "middle": "A", "last": "Smith"}}' # spread_values still gives the same column structure c(json, json2) \%>\% spread_values( - first.name = json_chr(name, first), - last.name = json_chr(name, last), - age = json_dbl(age) + first.name = jstring(name, first), + last.name = jstring(name, last), + age = jnumber(age) ) # whereas spread_all adds a new column @@ -68,6 +68,6 @@ c(json, json2) \%>\% spread_all \seealso{ \code{\link{spread_all}} for spreading all values, \code{\link[tidyr]{spread}} for spreading data frames, - \code{\link{json_chr}}, \code{\link{json_dbl}}, - \code{\link{json_lgl}} for accessing specific names + \code{\link{jstring}}, \code{\link{jnumber}}, + \code{\link{jlogical}} for accessing specific names } diff --git a/man/worldbank.Rd b/man/worldbank.Rd index 9210114..a4d6fd9 100644 --- a/man/worldbank.Rd +++ b/man/worldbank.Rd @@ -30,7 +30,7 @@ wb_sectors <- worldbank \%>\% # 500 Projects funded by the world bank select(project_name, regionname) \%>\% enter_object(majorsector_percent) \%>\% # Enter the 'sector' object gather_array("sector.index") \%>\% # Gather the array - spread_values(sector = json_chr(Name)) # Spread the sector name + spread_values(sector = jstring(Name)) # Spread the sector name # Examine the structured data wb_sectors \%>\% glimpse diff --git a/tests/testthat/test-enter_object.R b/tests/testthat/test-enter_object.R index 7e2a69d..8b5249d 100644 --- a/tests/testthat/test-enter_object.R +++ b/tests/testthat/test-enter_object.R @@ -50,7 +50,7 @@ test_that("filter removes records with missing path", { ) expect_identical( - json %>% spread_values(name = json_chr("name")) %>% + json %>% spread_values(name = jstring("name")) %>% enter_object("attributes"), tbl_json( data.frame( @@ -69,7 +69,7 @@ test_that("works if no paths exist", { json <- '{"name": "bob"}' expect_identical( - json %>% spread_values(name = json_chr("name")) %>% + json %>% spread_values(name = jstring("name")) %>% enter_object("attributes"), tbl_json( data.frame( diff --git a/tests/testthat/test-is_json.R b/tests/testthat/test-is_json.R index 1988b8e..f32448d 100644 --- a/tests/testthat/test-is_json.R +++ b/tests/testthat/test-is_json.R @@ -5,9 +5,9 @@ test_that("works for a simple example", { json <- '[1, "string", true, [1, 2], {"name": "value"}, null]' %>% gather_array - expect_identical(json %>% is_json_dbl %>% which, 1L) - expect_identical(json %>% is_json_chr %>% which, 2L) - expect_identical(json %>% is_json_lgl %>% which, 3L) + expect_identical(json %>% is_json_number %>% which, 1L) + expect_identical(json %>% is_json_string %>% which, 2L) + expect_identical(json %>% is_json_logical %>% which, 3L) expect_identical(json %>% is_json_array %>% which, 4L) expect_identical(json %>% is_json_object %>% which, 5L) expect_identical(json %>% is_json_null %>% which, 6L) @@ -31,13 +31,3 @@ test_that("works with filter", { ) }) - -test_that('deprecated functions warn appropriately', { - deptxt <- function(func,alt) { - paste0(func,'.*deprecated.*',alt,'.*instead') - } - - expect_warning(is_json_string('"a"'),deptxt('is_json_string','is_json_chr')) - expect_warning(is_json_number('2'),deptxt('is_json_number','is_json_dbl')) - expect_warning(is_json_logical('true'),deptxt('is_json_logical','is_json_lgl')) -}) \ No newline at end of file diff --git a/tests/testthat/test-spread_all.R b/tests/testthat/test-spread_all.R index afb9198..f9df27a 100644 --- a/tests/testthat/test-spread_all.R +++ b/tests/testthat/test-spread_all.R @@ -192,7 +192,7 @@ test_that("attr(.,JSON) remains intact", { "obj": {"a":2, "b": "test"}}' j <- json %>% spread_all(recursive=FALSE) %>% - spread_values(a=json_dbl(obj,a), b=json_chr(obj,b)) %>% + spread_values(a=jnumber(obj,a), b=jstring(obj,b)) %>% enter_object('hobby') %>% gather_array('hobbyid') %>% append_values_string('hobby') diff --git a/tests/testthat/test-spread_values.R b/tests/testthat/test-spread_values.R index a2b3d4a..16fbf7b 100644 --- a/tests/testthat/test-spread_values.R +++ b/tests/testthat/test-spread_values.R @@ -1,4 +1,4 @@ -context("json_chr") +context("jstring") test_that("works with simple input", { @@ -7,11 +7,11 @@ test_that("works with simple input", { fromJSON('{"name": {"first": "susan", "last": "jones"}}') ) - expect_identical(json_chr("name", "first")(json), c("bob", "susan")) - expect_identical(json_chr("name", "last")(json), c("smith", "jones")) + expect_identical(jstring("name", "first")(json), c("bob", "susan")) + expect_identical(jstring("name", "last")(json), c("smith", "jones")) - expect_identical(json_chr("name", "first", recursive=TRUE)(json), c("bob", "susan")) - expect_identical(json_chr("name", "last", recursive=TRUE)(json), c("smith", "jones")) + expect_identical(jstring("name", "first", recursive=TRUE)(json), c("bob", "susan")) + expect_identical(jstring("name", "last", recursive=TRUE)(json), c("smith", "jones")) } ) @@ -23,8 +23,8 @@ test_that("works with unquoted strings", { fromJSON('{"name": {"first": "susan", "last": "jones"}}') ) - expect_identical(json_chr(name, first)(json), c("bob", "susan")) - expect_identical(json_chr(name, last)(json), c("smith", "jones")) + expect_identical(jstring(name, first)(json), c("bob", "susan")) + expect_identical(jstring(name, last)(json), c("smith", "jones")) } ) @@ -39,13 +39,13 @@ test_that("handles missing input properly", { fromJSON('{}') ) - expect_identical(json_chr("name")(json), + expect_identical(jstring("name")(json), c("bob", "", NA_character_, NA_character_, NA_character_) ) } ) -context("json_dbl") +context("jnumber") test_that("handles missing input properly", { @@ -56,13 +56,13 @@ test_that("handles missing input properly", { fromJSON('{}') ) - expect_identical(json_dbl("age")(json), + expect_identical(jnumber("age")(json), c(32, NA_real_, NA_real_, NA_real_) ) } ) -context("json_lgl") +context("jlogical") test_that("handles missing input properly", { @@ -74,10 +74,10 @@ test_that("handles missing input properly", { fromJSON('{}') ) - expect_identical(json_lgl("is.past")(json), + expect_identical(jlogical("is.past")(json), c(TRUE, FALSE, NA, NA, NA) ) - expect_identical(json_lgl("is.past", recursive=TRUE)(json), + expect_identical(jlogical("is.past", recursive=TRUE)(json), c(TRUE, FALSE, NA, NA, NA) ) @@ -101,9 +101,9 @@ test_that("extract various values", { expect_identical( json %>% spread_values( - name = json_chr("name"), - age = json_dbl("age"), - customer = json_lgl("customer") + name = jstring("name"), + age = jnumber("age"), + customer = jlogical("customer") ), expected_value ) @@ -122,7 +122,7 @@ test_that("extract down a path", { expect_identical( json %>% - spread_values(first.name = json_chr("name", "first")), + spread_values(first.name = jstring("name", "first")), expected_value ) } @@ -138,7 +138,7 @@ test_that("correctly handles character(0)", { list()) expect_identical( - character(0) %>% spread_values(value = json_chr("name")), + character(0) %>% spread_values(value = jstring("name")), empty) } ) @@ -154,7 +154,7 @@ test_that("correctly handles {}", { stringsAsFactors = FALSE), list(nl)) - expect_identical('{}' %>% spread_values(value = json_chr("name")), empty) + expect_identical('{}' %>% spread_values(value = jstring("name")), empty) } ) @@ -168,31 +168,21 @@ test_that("correctly handles []", { stringsAsFactors = FALSE), list(list())) - expect_identical('[]' %>% spread_values(value = json_chr("name")), empty) + expect_identical('[]' %>% spread_values(value = jstring("name")), empty) } ) test_that('correctly handles over-specified path', { json <- '{ "a" : 1 , "b" : "text", "c" : true }' - expect_equal(json %>% spread_values(a = json_dbl("a", "b")) %>% .$a, as.numeric(NA)) + expect_equal(json %>% spread_values(a = jnumber("a", "b")) %>% .$a, as.numeric(NA)) - expect_equal(json %>% spread_values(b = json_chr('b','c')) %>% .$b, as.character(NA)) + expect_equal(json %>% spread_values(b = jstring('b','c')) %>% .$b, as.character(NA)) - expect_equal(json %>% spread_values(c = json_lgl('c','d')) %>% .$c, as.logical(NA)) + expect_equal(json %>% spread_values(c = jlogical('c','d')) %>% .$c, as.logical(NA)) }) -test_that('deprecated functions warn appropriately', { - deptxt <- function(func,alt) { - paste0(func,'.*deprecated.*',alt,'.*instead') - } - j <- '{"a":"one","b":2,"c":true}' - expect_warning(j %>% spread_values(a=jstring(a)),deptxt('jstring','json_chr')) - expect_warning(j %>% spread_values(b=jnumber(b)),deptxt('jnumber','json_dbl')) - expect_warning(j %>% spread_values(c=jlogical(c)),deptxt('jlogical','json_lgl')) -}) - context("recursive option") test_that("recursive works for simple input", { @@ -202,9 +192,9 @@ test_that("recursive works for simple input", { fromJSON('{"name": {"first": "susan", "last": "jones"}}') ) - expect_identical(json_chr("name", "first", recursive=TRUE)(json), + expect_identical(jstring("name", "first", recursive=TRUE)(json), c("bob", "susan")) - expect_identical(json_chr("name", "last", recursive=TRUE)(json), + expect_identical(jstring("name", "last", recursive=TRUE)(json), c("smith", "jones")) } @@ -217,9 +207,9 @@ test_that("recursive works for complex input", { fromJSON('{"name": {"first": "susan", "last": "jones"}}') ) - expect_identical(json_chr("name", "first", recursive=TRUE)(json), + expect_identical(jstring("name", "first", recursive=TRUE)(json), c("bob", "susan")) - expect_identical(json_chr("name", "last", recursive=TRUE)(json), + expect_identical(jstring("name", "last", recursive=TRUE)(json), c("smith", "jones")) json <- list( @@ -227,8 +217,8 @@ test_that("recursive works for complex input", { fromJSON('{"price": 30}') ) - expect_error(json_dbl("price")(json)) - expect_identical(json_dbl("price", recursive=TRUE)(json), c(30, 30)) + expect_error(jnumber("price")(json)) + expect_identical(jnumber("price", recursive=TRUE)(json), c(30, 30)) } ) @@ -240,9 +230,9 @@ test_that("recursive works for complex input and 2 levels of recursion", { fromJSON('{"name": {"first": "susan", "last": "jones"}}') ) - expect_identical(json_chr("name", "first", recursive=TRUE)(json), + expect_identical(jstring("name", "first", recursive=TRUE)(json), c("bob", "susan")) - expect_identical(json_chr("name", "last", recursive=TRUE)(json), + expect_identical(jstring("name", "last", recursive=TRUE)(json), c("smith", "jones")) json <- list( @@ -250,8 +240,8 @@ test_that("recursive works for complex input and 2 levels of recursion", { fromJSON('{"price": 30}') ) - expect_error(json_dbl("price")(json)) - expect_identical(json_dbl("price", recursive=TRUE)(json), c(30, 30)) + expect_error(jnumber("price")(json)) + expect_identical(jnumber("price", recursive=TRUE)(json), c(30, 30)) } ) @@ -262,14 +252,14 @@ test_that("recursive returns an error when multiple values are present", { fromJSON('{"name": {"first": "susan", "last": "jones"}}') ) - expect_error(json_chr("name", "first", recursive=TRUE)(json)) + expect_error(jstring("name", "first", recursive=TRUE)(json)) json <- list( fromJSON('{"price": {"value" : {"value1" : 30, "value2": 30}}}'), fromJSON('{"price": 30}') ) - expect_error(json_dbl("price", recursive=TRUE)(json)) + expect_error(jnumber("price", recursive=TRUE)(json)) } ) @@ -280,7 +270,7 @@ test_that("recursive works when nulls are present", { '{"name": {"last": "jones"}}') expect_identical( - (json %>% spread_values(name = json_chr("name", "first", recursive=TRUE)))$name, + (json %>% spread_values(name = jstring("name", "first", recursive=TRUE)))$name, c("bob", NA_character_)) json <- c('{"name": {"first": {"string1": "bob", "string2": "robert"}}, "last": "smith"}', @@ -294,22 +284,22 @@ test_that("either throws an error when type converting", { # Regular expect_error( - '{"name": "1"}' %>% spread_values(num = json_dbl("name")) + '{"name": "1"}' %>% spread_values(num = jnumber("name")) ) # Recursive expect_error( - '{"k1": {"k2": "1"}}' %>% spread_values(num = json_dbl("k1", recursive = TRUE)) + '{"k1": {"k2": "1"}}' %>% spread_values(num = jnumber("k1", recursive = TRUE)) ) }) test_that("works with x, json as input", { - expect_identical('{"x": 1}' %>% spread_values(x = json_chr("x")), - '{"x": 1}' %>% spread_values(y = json_chr("x")) %>% rename(x = y)) + expect_identical('{"x": 1}' %>% spread_values(x = jstring("x")), + '{"x": 1}' %>% spread_values(y = jstring("x")) %>% rename(x = y)) - expect_identical('{"json": 1}' %>% spread_values(json = json_chr("json")), - '{"json": 1}' %>% spread_values(y = json_chr("json")) %>% rename(json = y)) + expect_identical('{"json": 1}' %>% spread_values(json = jstring("json")), + '{"json": 1}' %>% spread_values(y = jstring("json")) %>% rename(json = y)) }) \ No newline at end of file diff --git a/tests/testthat/test-tbl_json.R b/tests/testthat/test-tbl_json.R index 86f48e0..d2ed4e4 100644 --- a/tests/testthat/test-tbl_json.R +++ b/tests/testthat/test-tbl_json.R @@ -130,15 +130,15 @@ test_that('functions as the identity on a simple pipeline', { test_that('functions as the identity on a more advanced pipeline', { x <- commits %>% gather_array() %>% spread_values( - sha=json_chr('sha') - , name=json_chr('commit','author','name') - , msg=json_chr('commit','message') - , comment_count=json_dbl('commit','comment_count') - , committer.name=json_chr('commit','committer','name') - , committer.date=json_chr('commit','committer','date') - , tree.sha=json_chr('committ','tree','sha') - , tree.url=json_chr('committ','tree','url') - , url=json_chr('url') + sha=jstring('sha') + , name=jstring('commit','author','name') + , msg=jstring('commit','message') + , comment_count=jnumber('commit','comment_count') + , committer.name=jstring('commit','committer','name') + , committer.date=jstring('commit','committer','date') + , tree.sha=jstring('committ','tree','sha') + , tree.url=jstring('committ','tree','url') + , url=jstring('url') ) expect_identical( @@ -266,7 +266,7 @@ test_that("works in a pipeline", { expect_identical( df %>% as.tbl_json(json.column = "json") %>% - spread_values(name = json_chr("name")) %>% + spread_values(name = jstring("name")) %>% dplyr::filter(age == 32) %>% `[[`("name"), "bob" @@ -320,10 +320,10 @@ test_that("[ column filtering doesn't change the JSON", { '{"name": "bob", "children": [{"name": "george"}]}', '{"name": "susan", "children": [{"name": "sally"}, {"name": "bobby"}]}' ) %>% as.tbl_json %>% - spread_values("parent" = json_chr("name")) %>% + spread_values("parent" = jstring("name")) %>% enter_object("children") %>% gather_array %>% - spread_values("child" = json_chr("name")) + spread_values("child" = jstring("name")) expect_identical( attr(x, "JSON"), @@ -338,7 +338,7 @@ test_that('handles "drop" like a tbl_df', { mydata <- as.tbl_json('[{"name": "Frodo", "occupation": "Ring Bearer"} ,{"name": "Aragorn", "occupation": "Ranger"}]') %>% gather_array() %>% - spread_values(name=json_chr('name'), occupation=json_chr('occupation')) + spread_values(name=jstring('name'), occupation=jstring('occupation')) expect_is(mydata[,],'tbl_json') expect_is(mydata[,'name'],'tbl_json') @@ -359,10 +359,10 @@ test_that('as_tibble drops the JSON attribute and tbl_json class', { test_that('as_data_frame functions like as_tibble', { jtidy <- issues %>% gather_array() %>% spread_values( - url=json_chr('url') - , body=json_chr('body') - , user.id=json_dbl('user.id') - , user.login=json_chr('user.login') + url=jstring('url') + , body=jstring('body') + , user.id=jnumber('user.id') + , user.login=jstring('user.login') ) expect_identical(attr(dplyr::as_data_frame(jtidy),'JSON'),NULL) @@ -393,11 +393,11 @@ test_that("dplyr::filter works in a more complex pipeline", { '{"name": "susan", "children": [{"name": "sally"}, {"name": "bobby"}]}' ) susan.children <- json %>% as.tbl_json %>% - spread_values(name = json_chr("name")) %>% + spread_values(name = jstring("name")) %>% dplyr::filter(name == "susan") %>% enter_object("children") %>% gather_array %>% - spread_values(child = json_chr("name")) + spread_values(child = jstring("name")) expect_identical(susan.children$child, c("sally", "bobby")) @@ -426,7 +426,7 @@ test_that("dplyr::mutate works with a simple example", { expect_identical( x %>% - spread_values(name = json_chr("name")) %>% + spread_values(name = jstring("name")) %>% dplyr::mutate(fullname = paste(name, "green")), tbl_json( dplyr::data_frame( @@ -447,11 +447,11 @@ test_that("dplyr::mutate works in a more complex pipeline", { '{"name": "susan", "children": [{"name": "sally"}, {"name": "bobby"}]}') children <- json %>% as.tbl_json %>% - spread_values(name = json_chr("name")) %>% + spread_values(name = jstring("name")) %>% dplyr::mutate(parent.rank = rank(name)) %>% enter_object("children") %>% gather_array %>% - spread_values(child = json_chr("name")) + spread_values(child = jstring("name")) expect_identical(children$parent.rank, c(1, 2, 2)) expect_identical(children$child, c("george", "sally", "bobby")) @@ -526,8 +526,8 @@ test_that("bind_rows works with tbl_json", { people_df <- people %>% gather_array %>% spread_values( - name = json_chr("name"), - age = json_dbl("age")) + name = jstring("name"), + age = jnumber("age")) z <- people_df %>% bind_rows(people_df) diff --git a/vignettes/multiple-apis.Rmd b/vignettes/multiple-apis.Rmd index a65011f..c70990e 100644 --- a/vignettes/multiple-apis.Rmd +++ b/vignettes/multiple-apis.Rmd @@ -38,17 +38,17 @@ dplyr_issues <- as.tbl_json(baseurl) dplyr_issues %>% json_schema %>% prettify ``` -After exploring the structure of the data, we decide we want to look at a high level overview of the isssues we have. Note that we can grab nested object detail by declaring a more complex path like `json_chr('assignee','login')`. This avoids the tendency to use `enter_object()` where it is not necessary. +After exploring the structure of the data, we decide we want to look at a high level overview of the isssues we have. Note that we can grab nested object detail by declaring a more complex path like `jstring('assignee','login')`. This avoids the tendency to use `enter_object()` where it is not necessary. ```{r gitapi_highlevel, echo=TRUE} highlevel <- dplyr_issues %>% gather_array('index') %>% - spread_values(id=json_dbl('id') - , assignee=json_chr('assignee','login') - , comments=json_dbl('comments') - , title=json_chr('title') - , state=json_chr('state') - , number=json_dbl('number') + spread_values(id=jnumber('id') + , assignee=jstring('assignee','login') + , comments=jnumber('comments') + , title=jstring('title') + , state=jstring('state') + , number=jnumber('number') ) print(highlevel) @@ -78,10 +78,10 @@ manyissues <- tidyjson::bind_rows(manyissues) ## Summarize status & users that create issues manyissues %>% gather_array('issue') %>% spread_values( - login=json_chr('user','login') - , comments=json_dbl('comments') - , issuenum = json_dbl('number') - , state = json_chr('state') + login=jstring('user','login') + , comments=jnumber('comments') + , issuenum = jnumber('number') + , state = jstring('state') ) %>% group_by(login, state) %>% summarize(issuecount=n()) %>% ungroup() %>% spread(state, issuecount, fill=0) %>% mutate(total=closed+open) %>% @@ -106,7 +106,7 @@ Let's explore the array, but store executionTime for later reference: ```{r citibike_prep, echo=TRUE} citibike_list <- citibike %>% - spread_values(execution=json_chr(executionTime)) %>% + spread_values(execution=jstring(executionTime)) %>% enter_object('stationBeanList') %>% gather_array('arrayid') citibike_list %>% @@ -118,12 +118,12 @@ citibike_list %>% The percentage availablity of bikes should be linearly correlated. I.e. 25% bikes available means 75% of docks available. ```{r citibike_available, echo=TRUE} citibike_available <- citibike_list %>% - spread_values(id=json_dbl(id) - , location=json_chr(location) - , lastCommunication=json_chr(lastCommunicationTime) - , availableBikes=json_dbl(availableBikes) - , availableDocks=json_dbl(availableDocks) - , totalDocks=json_dbl(totalDocks)) %>% + spread_values(id=jnumber(id) + , location=jstring(location) + , lastCommunication=jstring(lastCommunicationTime) + , availableBikes=jnumber(availableBikes) + , availableDocks=jnumber(availableDocks) + , totalDocks=jnumber(totalDocks)) %>% mutate(openDockPct=availableDocks / totalDocks , bikeDockPct=availableBikes / totalDocks , timeSinceUpdateMinutes=as.integer(as_datetime(execution)-as_datetime(lastCommunication))/60 @@ -151,8 +151,8 @@ ggplot(citibike_available, aes(availableBikes, availableDocks, col=timeSinceUpda Remember that our object is still a tbl_json object, so we can go back and grab additional keys if necessary. What if we wanted to map the data for easier use while we explore the city? ```{r citibike_map_prep, ECHO=TRUE} citibike_map <- citibike_available %>% - spread_values(lat=json_dbl(latitude) - , long=json_dbl(longitude)) + spread_values(lat=jnumber(latitude) + , long=jnumber(longitude)) citibike_map %>% group_by(is.na(lat),is.na(long)) %>% summarize(n()) ``` @@ -165,16 +165,16 @@ One last point of note. What if we got a bad response and our pipeline above wa ```{r citibike_error_test, ECHO=TRUE} citibike_list_0 <- '{}' %>% - spread_values(execution=json_chr(executionTime)) %>% + spread_values(execution=jstring(executionTime)) %>% enter_object('stationBeanList') %>% gather_array('arrayid') citibike_available_0 <- citibike_list_0 %>% - spread_values(id=json_dbl(id) - , location=json_chr(location) - , lastCommunication=json_chr(lastCommunicationTime) - , availableBikes=json_dbl(availableBikes) - , availableDocks=json_dbl(availableDocks) - , totalDocks=json_dbl(totalDocks)) %>% + spread_values(id=jnumber(id) + , location=jstring(location) + , lastCommunication=jstring(lastCommunicationTime) + , availableBikes=jnumber(availableBikes) + , availableDocks=jnumber(availableDocks) + , totalDocks=jnumber(totalDocks)) %>% mutate(openDockPct=availableDocks / totalDocks , bikeDockPct=availableBikes / totalDocks , timeSinceUpdateMinutes=as.integer(as_datetime(execution)-as_datetime(lastCommunication))/60 diff --git a/vignettes/visualizing-json.Rmd b/vignettes/visualizing-json.Rmd index 265a4ed..6a3bc10 100644 --- a/vignettes/visualizing-json.Rmd +++ b/vignettes/visualizing-json.Rmd @@ -301,7 +301,7 @@ Let's look at the most complex example: most_complex <- companies[which(co_length$complexity == max(co_length$complexity))] most_complex_name <- most_complex %>% - spread_values(name = json_chr(name)) %>% + spread_values(name = jstring(name)) %>% extract2("name") ``` @@ -348,9 +348,9 @@ rounds <- companies %>% enter_object(funding_rounds) %>% gather_array %>% spread_values( - round = json_chr(round_code), - currency = json_chr(raised_currency_code), - raised = json_dbl(raised_amount) + round = jstring(round_code), + currency = jstring(raised_currency_code), + raised = jnumber(raised_amount) ) rounds %>% head ``` @@ -362,9 +362,9 @@ geos <- companies %>% enter_object(offices) %>% gather_array %>% spread_values( - country = json_chr(country_code), - state = json_chr(state_code), - description = json_chr(description) + country = jstring(country_code), + state = jstring(state_code), + description = jstring(description) ) geos %>% head ``` From a4f9222f583bfe59dbad0a48ea5f54571d22ba22 Mon Sep 17 00:00:00 2001 From: Cole Arendt Date: Sat, 19 Aug 2017 16:20:53 -0400 Subject: [PATCH 4/4] Add osx builds Add R oldrel / devel builds Allow devel failures --- .travis.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.travis.yml b/.travis.yml index 42c4363..ab2352e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,19 @@ language: R sudo: false cache: packages +os: + - linux + - osx + +r: +- oldrel +- release +- devel + +matrix: + allow_failures: + - r: devel + r_packages: - covr