R/validate_import.R

Defines functions check_template_cols check_distinct_ous validate_import

Documented in check_distinct_ous check_template_cols validate_import

#' Validation on import
#'
#' Runs validation on data after reading in the dataset - making sure it has the
#' required column based on the template type and check if there is one ore more
#' operating units.
#'
#' @param df df create during `hfr_import()`
#'
#' @export
#' @family validation

validate_import <- function(df){

  check_template_cols(df)

  check_distinct_ous(df)

}


#' Check OUs listed in operatingunit
#'
#' @param df df created during `hfr_import()`
#'
#' @family internal

check_distinct_ous <- function(df){

  ous <- df %>%
    dplyr::distinct(operatingunit) %>%
    dplyr::pull(operatingunit) %>%
    paste(collapse = ", ")

  multi_ous <- length(ous) > 1

  ou_out <- ifelse(multi_ous == TRUE, crayon::yellow(ous), crayon::blue(ous))

  #print validation
  cat("\nIs there just one OU (for non regional OUs)?", ou_out)
}

#' Checks template's columns
#'
#' @param df df created during `hfr_import()`
#'
#' @family internal
#'
check_template_cols <- function(df){

  if(var_exists(df, "val")){
    req_cols <- template_cols_long
  } else if(var_exists(df, "hts_tst.NA.NA")){
    req_cols <- template_cols_wide_lim
  } else {
    req_cols <- template_cols_wide
  }

  missing <- flag_missing(req_cols, names(df))
  extra <- flag_extra(req_cols, names(df))

  #PRINT VALIDATION

  cat("\nAre there any missing columns on import?", missing,
      "\nAre there any extra columns on import?", extra)
}
USAID-OHA-SI/Wavelength documentation built on March 24, 2023, 10:07 a.m.