Skip to content

Commit

Permalink
Merge pull request #5 from colearendt/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
colearendt authored Aug 26, 2017
2 parents 168b4d7 + 5cdc59f commit d661b42
Show file tree
Hide file tree
Showing 33 changed files with 226 additions and 342 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 0 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -31,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)
Expand All @@ -42,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)
Expand Down
8 changes: 0 additions & 8 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +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)

* `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
Expand Down
35 changes: 7 additions & 28 deletions R/append_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,17 +30,17 @@
#' # 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:
#' 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_dbl("count")
#' append_values_number("count")
NULL

#' Creates the append_values_* functions
Expand Down Expand Up @@ -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)
}
append_values_logical <- append_values_factory("logical", as.logical)
2 changes: 1 addition & 1 deletion R/data-issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/data-worldbank.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions R/enter_object.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}'
Expand Down
10 changes: 5 additions & 5 deletions R/gather.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}]'
Expand All @@ -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
Expand Down
29 changes: 4 additions & 25 deletions R/is_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions R/spread_all.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>%
Expand Down
54 changes: 17 additions & 37 deletions R/spread_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion R/tbl_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit d661b42

Please sign in to comment.