R/collect.R

Defines functions transfer_groups collect.GroupedCrunchDataset collect.CrunchDataset

Documented in collect.CrunchDataset collect.GroupedCrunchDataset

#' Collect a Crunch dataset from the server
#'
#' This function brings a Crunch dataset into memory so that you can
#' work with the data using R functions. Since this can create a long running
#' query it is recommended that you try to filter the dataset down as much as possible
#' before running `collect()`.
#'
#' When collecting a grouped CrunchDataset, the grouping will be preserved.
#'
#' @param x A Crunch Dataset
#' @param ... Other arguments passed to `crunch::as.data.frame()`
#' @return A `tbl_df` or `grouped_df`
#' @export
#' @importFrom tibble as_tibble
#' @importFrom dplyr collect
#' @name collect
#'
#' @examples
#' \dontrun{
#' ds %>%
#'    group_by(cyl) %>%
#'    select(cyl, gear) %>%
#'    collect()
#' }
collect.CrunchDataset <- function(x, ...) {
    as_tibble(as.data.frame(x, force = TRUE, ...))
}

#' @rdname collect
#' @export
collect.GroupedCrunchDataset <- function(x, ...) {
    return(transfer_groups(collect.CrunchDataset(x, ...), x))
}

# Broken out for testing
transfer_groups <- function(df, grouped_df) {
    return(group_by(df, !!!syms(group_vars(grouped_df))))
}

Try the crplyr package in your browser

Any scripts or data that you put into this service are public.

crplyr documentation built on March 31, 2023, 9:30 p.m.