R/08_gatherx.R

#' Enhanced gather supporting keep argument
#'
#' @inheritParams tidyr::gather
#' @param keep columns to keep, additionally to columns mentionned in \code{...}.
#' Passed to \code{select} so supports select helper, including tidyx helper
#' \code{nothing()}.
#'
#' @export
gatherx <- function(data, key = "key", value = "value", ..., na.rm = FALSE,
                    convert = FALSE, factor_key = FALSE, keep = dplyr::everything()){
  key <- rlang::enquo(key)
  value <- rlang::enquo(value)
  keep <- rlang::enquo(keep)
  data <- data %>% dplyr::select(..., !!keep)
  tidyr::gather(data, !!key, !!value, ...,
                na.rm = na.rm, convert = convert, factor_key = factor_key)
}

#' Select helper to select nothing
#'
#' @export
nothing <- function(){
  integer(0)
}
moodymudskipper/tidyx documentation built on May 17, 2019, 10:39 a.m.