R/get_group_n.R

Defines functions get_group_n

Documented in get_group_n

#' @title Formatted Group Sample Size for Tables
#'
#' @description Given a tibble and a filter expression, get_group_n returns
#'   the group sample size formatted as "N = XXXX". Made to work in a dplyr
#'   pipeline, and used when creating tables for publications / reports.
#'
#' @param .data A data frame or tibble
#' @param ... A dplyr::filter expression. Used to select subgroup.
#'
#' @return A character string
#' @export
#'
#' @examples
#' library(tidyverse)
#' library(bfuncs)
#'
#' data(mtcars)
#'
#' # Get sample size for cars with 4 cylinders
#' mtcars %>% get_group_n(cyl == 4)
#'
#' #> [1] "N = 11"
get_group_n <- function(.data, ...) {

  # ------------------------------------------------------------------
  # Prevents R CMD check: "no visible binding for global variable ‘.’"
  # ------------------------------------------------------------------
  n = NULL

  # Turn filter expression into a quoture
  filter_exp <- rlang::quos(...)

  # Filter .data by the filter expression (e.g., cyl == 4)
  # Count number of remaining rows
  # Format number of rows as: N = XX
  # Return as a character vector
  .data %>%
    dplyr::filter(!!!filter_exp) %>%
    dplyr::summarise(n = n()) %>%
    dplyr::mutate(n = paste0("N = ", format(n, big.mark = ","))) %>%
    dplyr::pull(n)
}
brad-cannell/my_functions documentation built on July 25, 2019, 4:29 p.m.