R/table_utils.R

Defines functions fmt_p fmt prettify_number

prettify_number <- function(d, number = T, percent = T, ...) {
  if (percent) {
    query <- "{fmt_p(x/sum(x))}"
  }

  if (number) {
    query <- sprintf("{fmt(x)} (%s)", stringr::str_glue(query))
  }

  d %>%
    dplyr::select(...) %>%
    dplyr::mutate_if(is.numeric, .funs = function(x) {
      stringr::str_glue(query)
    }) %>%
    dplyr::as_data_frame()
}

# adapted from plyr package
fmt <- function(x) {
  format(x,
    big.mark = ".", small.mark = ",",
    decimal.mark = ",",
    scientific = FALSE, trim = TRUE
  )
}

# adapted from plyr package
fmt_p <- function(x) {
  if (length(x) == 0) {
    return(character())
  }
  accuracy <- precision(x) / 100
  x <- round(x / accuracy) * accuracy
  x <- fmt(x * 100)
  paste0(x, "%")
}

Try the abjutils package in your browser

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

abjutils documentation built on Feb. 1, 2022, 9:10 a.m.