Skip to content

Commit

Permalink
Merge pull request #28 from sailthru/df_to_json
Browse files Browse the repository at this point in the history
Implement as.tbl_json.data.frame, closes #27
  • Loading branch information
Jeremy Stanley committed Apr 3, 2015
2 parents e2f6ce4 + fd10eb6 commit 2a65060
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 28 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
S3method("[",tbl_json)
S3method(arrange_,tbl_json)
S3method(as.tbl_json,character)
S3method(as.tbl_json,data.frame)
S3method(as.tbl_json,tbl_json)
S3method(filter_,tbl_json)
S3method(mutate_,tbl_json)
Expand Down
21 changes: 21 additions & 0 deletions R/tbl_json.r
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ as.tbl_json.character <- function(x, ...) {
tbl_json(ids, json)
}

#' Turns a data.frame into a tbl_json object
#' @param x data.frame that has a column of JSON data
#' @param json.column the name of the JSON column of data in x, if x is a data.frame
#' @rdname tbl_json
#' @export
as.tbl_json.data.frame <- function(x, json.column, ...) {

assert_that(is.character(json.column))
assert_that(json.column %in% names(x))

# Parse the json
json <- lapply(x[[json.column]], fromJSON, simplifyVector = FALSE)

# Remove json column
x <- x[, setdiff(names(x), json.column), drop = FALSE]

# Construct tbl_json
tbl_json(x, json)

}

#' @rdname tbl_json
#' @export
is.tbl_json <- function(x) inherits(x, "tbl_json")
Expand Down
1 change: 0 additions & 1 deletion man/allowed_json_types.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/json_types.r
\docType{data}
\name{allowed_json_types}
\alias{allowed_json_types}
Expand Down
1 change: 0 additions & 1 deletion man/append_values.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/append_values.r
\name{append_values}
\alias{append_values}
\alias{append_values_logical}
Expand Down
1 change: 0 additions & 1 deletion man/append_values_factory.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/append_values.r
\name{append_values_factory}
\alias{append_values_factory}
\title{Creates the append_values_* functions}
Expand Down
1 change: 0 additions & 1 deletion man/append_values_type.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/append_values.r
\name{append_values_type}
\alias{append_values_type}
\title{get list of values from json}
Expand Down
1 change: 0 additions & 1 deletion man/commits.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/data-commits.r
\docType{data}
\name{commits}
\alias{commits}
Expand Down
1 change: 0 additions & 1 deletion man/companies.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/data-companies.r
\docType{data}
\name{companies}
\alias{companies}
Expand Down
1 change: 0 additions & 1 deletion man/determine_types.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/json_types.r
\name{determine_types}
\alias{determine_types}
\title{Determines the types of a list of parsed JSON}
Expand Down
1 change: 0 additions & 1 deletion man/enter_object.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/enter_object.r
\name{enter_object}
\alias{enter_object}
\title{Selects an object by key and filters rows to just those with matching keys}
Expand Down
1 change: 0 additions & 1 deletion man/gather_array.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/gather_array.r
\name{gather_array}
\alias{gather_array}
\title{Expands a tbl_json to span the indices of a JSON array}
Expand Down
1 change: 0 additions & 1 deletion man/gather_keys.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/gather_keys.r
\name{gather_keys}
\alias{gather_keys}
\title{Gathers every key from the top level of the json and stacks them}
Expand Down
1 change: 0 additions & 1 deletion man/issues.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/data-issues.r
\docType{data}
\name{issues}
\alias{issues}
Expand Down
1 change: 0 additions & 1 deletion man/jfactory.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/spread_values.r
\name{jfactory}
\alias{jfactory}
\title{Factory that creates the j* functions below}
Expand Down
1 change: 0 additions & 1 deletion man/jfunctions.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/spread_values.r
\name{jfunctions}
\alias{jfunctions}
\alias{jlogical}
Expand Down
1 change: 0 additions & 1 deletion man/json_lengths.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/json_lengths.r
\name{json_lengths}
\alias{json_lengths}
\title{Add a column that tells the 'length' of the data in the root of the JSON}
Expand Down
1 change: 0 additions & 1 deletion man/json_types.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/json_types.r
\name{json_types}
\alias{json_types}
\title{Add a column that tells the 'type' of the data in the root of the JSON}
Expand Down
1 change: 0 additions & 1 deletion man/list_path.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/path.r
\name{list_path}
\alias{list_path}
\title{Recursively access a path}
Expand Down
1 change: 0 additions & 1 deletion man/my_unlist.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/append_values.r
\name{my_unlist}
\alias{my_unlist}
\title{Unlists while preserving NULLs and only unlisting lists with one value}
Expand Down
1 change: 0 additions & 1 deletion man/prep_path.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/path.r
\name{prep_path}
\alias{prep_path}
\title{Prepare a path from ...}
Expand Down
1 change: 0 additions & 1 deletion man/replace_nulls.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/path.r
\name{replace_nulls}
\alias{replace_nulls}
\title{Replace nulls with something else}
Expand Down
1 change: 0 additions & 1 deletion man/spread_values.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/spread_values.r
\name{spread_values}
\alias{spread_values}
\title{Extracts values from JSON refereced by a sequence of keys}
Expand Down
1 change: 0 additions & 1 deletion man/sub-.tbl_json.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/tbl_json.r
\name{[.tbl_json}
\alias{[.tbl_json}
\title{Extract subsets of a tbl_json object (not replace)}
Expand Down
12 changes: 10 additions & 2 deletions man/tbl_json.Rd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/tbl_json.r
\name{tbl_json}
\alias{as.tbl_json}
\alias{as.tbl_json.character}
\alias{as.tbl_json.data.frame}
\alias{as.tbl_json.tbl_json}
\alias{is.tbl_json}
\alias{tbl_json}
Expand All @@ -16,6 +16,8 @@ as.tbl_json(x, ...)

\method{as.tbl_json}{character}(x, ...)

\method{as.tbl_json}{data.frame}(x, json.column, ...)

is.tbl_json(x)
}
\arguments{
Expand All @@ -25,9 +27,13 @@ is.tbl_json(x)

\item{drop.null.json}{drop NULL json entries from data.frame and json}

\item{...}{other arguments}

\item{x}{character vector of json}

\item{...}{other arguments}
\item{x}{data.frame that has a column of JSON data}

\item{json.column}{the name of the JSON column of data in x, if x is a data.frame}
}
\description{
Combines structured JSON (as a data.frame) with remaining JSON
Expand All @@ -38,5 +44,7 @@ note that "..JSON" is a reserved column name used internally for filtering
tbl_json objects, and so is not allowed in the data.frame names.

Turns a character vector into a tbl_json object

Turns a data.frame into a tbl_json object
}

1 change: 0 additions & 1 deletion man/tidyjson.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/tidyjson-package.r
\docType{package}
\name{tidyjson}
\alias{tidyjson}
Expand Down
1 change: 0 additions & 1 deletion man/worldbank.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/data-worldbank.r
\docType{data}
\name{worldbank}
\alias{worldbank}
Expand Down
1 change: 0 additions & 1 deletion man/wrap_dplyr_verb.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/tbl_json.r
\name{wrap_dplyr_verb}
\alias{wrap_dplyr_verb}
\title{Wrapper for extending dplyr verbs to tbl_json objects}
Expand Down
55 changes: 53 additions & 2 deletions tests/testthat/test-tbl_json.r
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,59 @@ test_that("throws error on invalid json", {
}
)

context("tbl_json: as.tbl_json.data.frame")

test_that("works for a data.frame and data_frame created objects", {

df <- data.frame(
document.id = 1:2,
json = c('{"name": "bob"}', '{"name": "susan"}'),
stringsAsFactors = FALSE)
# data.frame
expect_identical(
as.tbl_json(df, json.column = "json"),
as.tbl_json(df$json)
)
# data_frame
df <- data_frame(
document.id = 1:2,
json = c('{"name": "bob"}', '{"name": "susan"}'))
expect_identical(
as.tbl_json(df, json.column = "json"),
as.tbl_json(df$json)
)

}
)

test_that("works in a pipeline", {

df <- data_frame(
age = c(32, 45),
json = c('{"name": "bob"}', '{"name": "susan"}')
)

expect_identical(
df %>% as.tbl_json(json.column = "json") %>%
spread_values(name = jstring("name")) %>%
filter(age == 32) %>%
`[[`("name"),
"bob"
)

}
)

test_that("throws an error without json.column specified", {
expect_error(as.tbl_json(iris))
}
)

test_that("throws an error if json column doesn't exist", {
expect_error(as.tbl_json(iris, json.column = "json"))
}
)

context("tbl_json")

test_that("tbl_json constructor works with no data", {
Expand Down Expand Up @@ -164,7 +217,6 @@ test_that("dplyr::arrange works with a simple example", {
}
)


test_that("dplyr::mutate works with a simple example", {

x <- as.tbl_json(c('{"name": "bob"}', '{"name": "susan"}'))
Expand All @@ -185,7 +237,6 @@ test_that("dplyr::mutate works with a simple example", {
}
)


test_that("dplyr::mutate works in a more complex pipeline", {

json <- c(
Expand Down

0 comments on commit 2a65060

Please sign in to comment.