Skip to content

Commit

Permalink
Merge pull request #24 from sailthru/mutate
Browse files Browse the repository at this point in the history
adds better support for dplyr::mutate + tests
  • Loading branch information
Jeremy Stanley committed Mar 5, 2015
2 parents bbcfc87 + b46807f commit e2f6ce4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(arrange_,tbl_json)
S3method(as.tbl_json,character)
S3method(as.tbl_json,tbl_json)
S3method(filter_,tbl_json)
S3method(mutate_,tbl_json)
export(append_values_logical)
export(append_values_number)
export(append_values_string)
Expand Down
5 changes: 4 additions & 1 deletion R/data-companies.r
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#' spread_values(
#' name = jstring("name")
#' ) %>%
#' mutate(
#' company.sort_order = rank(name)
#' ) %>%
#' enter_object("relationships") %>%
#' gather_array("relationship.index") %>%
#' spread_values(
Expand Down Expand Up @@ -46,4 +49,4 @@
#' ggplot(comp_type_counts, aes(key, n, fill = type)) +
#' geom_bar(position = "stack", stat = "identity") +
#' coord_flip()
NULL
NULL
3 changes: 3 additions & 0 deletions R/tbl_json.r
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,6 @@ filter_.tbl_json <- wrap_dplyr_verb(dplyr::filter_)

#' @export
arrange_.tbl_json <- wrap_dplyr_verb(dplyr::arrange_)

#' @export
mutate_.tbl_json <- wrap_dplyr_verb(dplyr::mutate_)
3 changes: 3 additions & 0 deletions man/companies.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ key_employees <- companies \%>\%
spread_values(
name = jstring("name")
) \%>\%
mutate(
company.sort_order = rank(name)
) \%>\%
enter_object("relationships") \%>\%
gather_array("relationship.index") \%>\%
spread_values(
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-tbl_json.r
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,44 @@ 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"}'))

expect_identical(
x %>%
spread_values(name = jstring("name")) %>%
mutate(fullname = paste(name, "green")),
tbl_json(
data_frame(
document.id = c(1L, 2L),
name = c("bob", "susan"),
fullname = c("bob green", "susan green")),
list(list(name = "bob"), list(name = "susan"))
)
)

}
)


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

json <- c(
'{"name": "bob", "children": [{"name": "george"}]}',
'{"name": "susan", "children": [{"name": "sally"}, {"name": "bobby"}]}')

children <- json %>% as.tbl_json %>%
spread_values(name = jstring("name")) %>%
mutate(parent.rank = rank(name)) %>%
enter_object("children") %>%
gather_array %>%
spread_values(child = jstring("name"))

expect_identical(children$parent.rank, c(1, 2, 2))
expect_identical(children$child, c("george", "sally", "bobby"))

}
)

0 comments on commit e2f6ce4

Please sign in to comment.