R/tidyr.R

#' @importFrom tidyr spread
#' @export
spread.tbl_lazy <- function(data, key, value, fill=NULL, ...){
    key   <- rlang::enquo(key)
    value <- rlang::enquo(value)
    data  %>%
        dplyr::group_by_(.dots =
             tidyselect::vars_select( dplyr::tbl_vars(data)
                                    , tidyselect::everything()
                                    , .exclude = c( dplyr::select_var(dplyr::tbl_vars(data), !!key)
                                                  , all_names(value)
                                                  ))) %>%
        pivot(!!key, !!value, tidyselect::everything(), fill=fill)
}

#' @importFrom tidyr gather
#' @export
gather.tbl_lazy <- function(data, key='key', value='value', ...){
    key   <- rlang::enquo(key)
    value <- rlang::enquo(value)
    dots  <- rlang::quos(...)
    unpivot(data, key, value, !!!dots)
}

Try the pivot package in your browser

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

pivot documentation built on May 2, 2019, 2:02 p.m.