R/filter.R

Defines functions totals_value filter_remove_totals filter_for_totals

Documented in filter_for_totals filter_remove_totals totals_value

#' Filter for Totals Rows
#'
#' @description Filter a data frame for rows with the "TOTAL" value
#'
#' @param data A data frame
#' @param dims Character vector of columns to filter for totals in
#' @param tv Character, totals value. See ?totals_value
#'
#' @return Data frame
#'
#' @export
filter_for_totals <- function(data, dims, tv = totals_value()) {
  dplyr::filter(
    data,
    dplyr::if_any(dplyr::any_of(dims), ~ . == tv)
  )
}



#' Filter for Non-Totals Rows
#'
#' @description Filter a data frame to remove rows with the "TOTAL" value
#'
#' @param data A data frame
#' @param dims Character vector of columns to remove totals rows from
#' @param tv Character, totals value. See ?totals_value
#'
#' @return Data frame
#'
#' @export
filter_remove_totals <- function(data, dims, tv = totals_value()) {
  dplyr::filter(
    data,
    dplyr::if_all(dplyr::any_of(dims), ~ . != tv)
  )
}


#' Value to Use as Indicator of a Rollup
#'
#' @param dq Logical, whether or not to wrap in single quotes as well as double
#'   quotes
#'
#' @return Character
#'
#' @export
totals_value <- function(dq = FALSE) {
  ifelse(dq, "'TOTAL'", "TOTAL")
}
EricLamphere/ezxfig documentation built on Jan. 29, 2023, 1:44 a.m.