R/format_numeric.R

Defines functions fmt_percent_by_col fmt_percent_by_row fmt_percent_by_row_and_col fmt_value_by_col fmt_value_by_row fmt_value_by_row_and_col

#' Format Flextable Values (Row and Column)
#'
#' @param flex flextable
#' @param cols numeric
#' @param rows numeric
#'
#' @return flextable
#' @export
#'
#'
fmt_value_by_row_and_col <- function(flex, cols, rows) {
  nc <- numeric_cols(flex$body$dataset)
  nc <- flex$col_keys[which(flex$col_keys %in% nc)]
  if (is.numeric(cols)) cols <- flex_define_col_keys(flex, cols)
  if (!all(cols %in% nc))
    stop("Cannot apply a numeric format to a non-numeric column.")
  
  colformat_num(
    flex,
    i        = rows,
    col_keys = cols,
    digits   = 0,
    na_str   = "---"
  )
}

#' Format Flextable Values (Row)
#'
#' @param flex flextable
#' @param rows numeric
#'
#' @return flextable
#' @export
#'
#'
fmt_value_by_row <- function(flex, rows) {
  nc <- numeric_cols(flex$body$dataset)
  cols <- flex$col_keys[which(flex$col_keys %in% nc)]
  
  colformat_num(
    flex,
    i        = rows,
    col_keys = cols,
    digits   = 0,
    na_str   = "---"
  )
}

#' Format Flextable Values (Column)
#'
#' @param flex flextable
#' @param cols numeric
#'
#' @return flextable
#' @export
#'
#'
fmt_value_by_col <- function(flex, cols) {
  nc <- numeric_cols(flex$body$dataset)
  nc <- flex$col_keys[which(flex$col_keys %in% nc)]
  if (is.numeric(cols)) cols <- flex_define_col_keys(flex, cols)
  if (!all(cols %in% nc))
    stop("Cannot apply a numeric format to a non-numeric column.")
  
  colformat_num(
    flex,
    i        = 1:nrow(flex$body$dataset),
    col_keys = cols,
    digits   = 0,
    na_str   = "---"
  )
}

#' Format Flextable Percent (Row and Column)
#'
#' @param flex flextable
#' @param cols numeric
#' @param rows numeric
#'
#' @return flextable
#' @export
#'
#'
fmt_percent_by_row_and_col <- function(flex, cols, rows) {
  nc <- numeric_cols(flex$body$dataset)
  nc <- flex$col_keys[which(flex$col_keys %in% nc)]
  if (is.numeric(cols)) cols <- flex_define_col_keys(flex, cols)
  if (!all(cols %in% nc))
    stop("Cannot apply a numeric format to a non-numeric column.")
  
  colformat_num(
    flex,
    i        = rows,
    col_keys = cols,
    digits   = 1,
    na_str   = "---",
    suffix   = "%"
  )
}

#' Format Flextable Percent (Row)
#'
#' @param flex flextable
#' @param rows numeric
#'
#' @return flextable
#' @export
#'
#'
fmt_percent_by_row <- function(flex, rows) {
  nc   <- numeric_cols(flex$body$dataset)
  cols <- flex$col_keys[which(flex$col_keys %in% nc)]
  
  colformat_num(
    flex,
    i        = rows,
    col_keys = cols,
    digits   = 1,
    na_str   = "---",
    suffix   = "%"
  )
}

#' Format Flextable Percent (Column)
#'
#' @param flex flextable
#' @param cols numeric
#'
#' @return flextable
#' @export
#' 
#'
fmt_percent_by_col <- function(flex, cols) {
  nc <- numeric_cols(flex$body$dataset)
  nc <- flex$col_keys[which(flex$col_keys %in% nc)]
  if (is.numeric(cols)) cols <- flex_define_col_keys(flex, cols)
  if (!all(cols %in% nc))
    stop("Cannot apply a numeric format to a non-numeric column.")
  
  colformat_num(
    flex,
    i        = 1:nrow(flex$body$dataset),
    col_keys = cols,
    digits   = 1,
    na_str   = "---",
    suffix   = "%"
  )
} 
cadenceinc/FlextableExtended documentation built on May 28, 2020, 12:49 a.m.