R/etc.R

Defines functions check_missing overwrite_col check_type check_col

Documented in check_col check_type overwrite_col

# Internal ----------------------------------------------------------------
# Alec Robitaille

#' check col
#' @param DT data.table
#' @param col column name
#' @param arg argument name
#' @param extra extras
check_col <- function(DT = NULL, col = NULL, arg = NULL, extra = NULL) {

  if (is.null(arg)) {
    it <- col
  } else {
    it <- paste0(arg, " ('", col, "')")
  }

  if (!(col %in% colnames(DT))) {
    stop(paste0(it, ' column not found in DT', extra))
  }
}

#' check type
#' @param DT data.table.
#' @param col column name.
#' @param type typeof type type typeof type.
check_type <- function(DT = NULL, col = NULL, type = NULL) {
  if (!(typeof(DT[[col]]) %in% type)) {
    stop(paste0(col, ' does not match required type: ', type))
  }
}


#' overwite_col
#' @param DT data.table
#' @param col column name
overwrite_col <- function(DT = NULL, col = NULL) {

  if (col %in% colnames(DT)) {
    warning(paste0('overwriting ', col, ' column'))
    set(DT, j = eval(col), value = NULL)
  }
}

check_missing <- function(x, name) {
  if (missing(x)) {
    stop('must provide ', name)
  }
}
robitalec/prepare-locs documentation built on Jan. 30, 2024, 9:45 a.m.