R/hfr_process_template.R

Defines functions hfr_process_template

Documented in hfr_process_template

#' Import and Munge HFR Standard Templates
#'
#' Run validation check on country HFR submission while it processes and tidies
#' the submission for inclusion into the database.
#'
#' @param filepath filepath to sumbited template
#' @param round_hfrdate rounds date to the nearest HFRweek start (for non-compliance), default = FALSE
#' @param hfr_pd_sel filter for HFR reporting period, 1-13, no filter when NULL, default = NULL
#' @param folderpath_output if a csv output is desired, provide the full path to the folder
#' @param datim_path path to DATIM lookup files for full validation
#'
#' @export
#' @examples
#' \dontrun{
#' #file path for the
#'   path <- "~/WeeklyData/Saturn"
#'   output_folder <- "~/WeeklyData/Output"
#' #process Excel file for Saturn
#'   hfr_process_template(path, output_folder)
#' #process Excel file for Saturn with full validation
#'   datim_folder <- "~/Datim"
#'   hfr_process_template(path, output_folder, datim_path = datim_folder)
#' }
hfr_process_template <- function(filepath,
                                 round_hfrdate = FALSE,
                                 hfr_pd_sel = NULL,
                                 folderpath_output = NULL,
                                 datim_path = NULL){

  #validation checks
    validate_initial(filepath)

  #import template sheet(s)
    df <- hfr_import(filepath)

  #validation checks
    validate_import(df)

  #remove any extra columns
    df <- hfr_restrict_cols(df)

  #reshape wide to match long df (only affects wide format)
    df <- hfr_gather(df)

  #clean up string variables
    df <- hfr_munge_string(df)

  #resolve issues with non-standard data entry
    df <- hfr_fix_noncompliance(df)

  #adjust date & assign fy + pd
    df <- df %>%
      hfr_fix_date(round_hfrdate) %>%
      hfr_assign_pds()

  #filter if required to specific pd
    df <- hfr_filter_pd(df, hfr_pd_sel, hfr_fy_sel = curr_fy)

  #aggregate to combine rows where needed (minimize row count)
    df <- hfr_aggr(df)

  #validation checks: full checks if datim_path provided
    df <- validate_output(df,
                          output_path=folderpath_output,
                          datim_path = path_files)

  #export
    hfr_export(df, folderpath_output, by_mech = TRUE)

  invisible(df)
}
USAID-OHA-SI/Wavelength documentation built on March 24, 2023, 10:07 a.m.