diff --git a/NAMESPACE b/NAMESPACE index 98d11f4..c6bcdd4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/data-companies.r b/R/data-companies.r index 8fc6482..158f280 100644 --- a/R/data-companies.r +++ b/R/data-companies.r @@ -19,6 +19,9 @@ #' spread_values( #' name = jstring("name") #' ) %>% +#' mutate( +#' company.sort_order = rank(name) +#' ) %>% #' enter_object("relationships") %>% #' gather_array("relationship.index") %>% #' spread_values( @@ -46,4 +49,4 @@ #' ggplot(comp_type_counts, aes(key, n, fill = type)) + #' geom_bar(position = "stack", stat = "identity") + #' coord_flip() -NULL \ No newline at end of file +NULL diff --git a/R/tbl_json.r b/R/tbl_json.r index c957234..2b83021 100644 --- a/R/tbl_json.r +++ b/R/tbl_json.r @@ -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_) diff --git a/man/companies.Rd b/man/companies.Rd index 00c8895..93f5354 100644 --- a/man/companies.Rd +++ b/man/companies.Rd @@ -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( diff --git a/tests/testthat/test-tbl_json.r b/tests/testthat/test-tbl_json.r index fb67db0..63fa75c 100644 --- a/tests/testthat/test-tbl_json.r +++ b/tests/testthat/test-tbl_json.r @@ -163,3 +163,31 @@ 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"}')) + + result <- x %>% + spread_values(name = jstring("name")) %>% + mutate( + name = factor(name), + newcol = c(4,5), + fullname = factor(paste(name, "green")) + ) + + expect_identical( + result, + tbl_json( + data.frame( + document.id = c(1L, 2L), + name = c("bob", "susan"), + newcol = c(4, 5), + fullname = c("bob green", "susan green")), + list(list(name = "bob"), list(name = "susan")) + ) + ) + + } +)