R/hash.R

hash <- fastdigest::fastdigest


#' Calculate hashes for elements of a list
#'
#' Hashes are calculated using \code{\link[fastdigest]{fastdigest}}
#' function from fastdigest package.
#'
#' @param .data a list.
#' @param \dots variables to group by.
#' @param warn  throw warnings when encountering errors in selecting variables
#'              (\code{FALSE} by default).
#'
#' @examples
#'
#' as_lol(mtcars) %>%
#'   hash_by(cyl, mpg)
#'
#' @export

hash_by <- function(.data, ..., warn = TRUE) UseMethod("hash_by")

#' @export

hash_by.list <- function(.data, ..., warn = TRUE) {
  dots <- quos(...)
  if (length(dots) == 0L)
    return(map_chr(.data, ~ hash(.x)))
  map_chr(.data, function(.x) {
    vars <- try_select_vars(names(.x), dots, warn = warn)
    hash(.x[vars])
  })
}


# this is not needed
#
# #' @rdname hash_by
# #' @export
#
# hash_by_ <- function(.data, by) UseMethod("hash_by")
#
# #' @export
#
# hash_by_.list <- function(.data, by) {
#   if (missing(by) || is.null(by) || length(by) == 0L)
#     return(map_chr(.data, ~ hash(.x)))
#   map_chr(.data, ~ hash(.x[by]))
# }
twolodzko/lolplyr documentation built on May 14, 2019, 8:22 a.m.