R/nhs_check.R

Defines functions nhs_check

Documented in nhs_check

#' Check error files
#' The error file is defined by the absence of the following five formats: xpt or sas7bdat,
#' tsv, varLabel, codebook and update.
#' @param years one or more years
#' @param items one or more items
#'
#' @return if error files exist, green text will be print.
#' @export
#'
#' @examples
#' \donttest{
#' nhs_check()
#' }
nhs_check <- function(years,items){
    (years <- prepare_years(years))
    (items <- prepare_items(items))
    (dt <- rep(items,each=length(years)))
    (ys <- rep(years,length(items)))
    fmt <- paste0(get_config_path(),'/%s/%s')
    (nhs_dir <- sprintf(fmt,ys,dt) |> do::increase())
    ext <- c("\\.codebook", "\\.varLabel", "\\.tsv", "\\.update", "\\.xpt","\\.sas7bdat")
    for (i in 1:length(nhs_dir)) {
        error=1
        (yeari <- prepare_years(nhs_dir[i]))
        (itemi <- prepare_items(nhs_dir[i]))
        if (i==1){
            cat('\n',yeari)
        }else{
            (yeari1 <- prepare_years(nhs_dir[i-1]))
            if (yeari != yeari1) cat('\n',yeari)
        }
        fn <- list.files(nhs_dir[i],paste0(ext,collapse = '|')) |>
            do::Replace0(ext) |>
            unique()
        fn
        j=which(fn=='dsbi')
        if (length(fn)==0) next(i)
        for (j in 1:length(fn)) {
            fnj <- fn[j]
            len <- list.files(nhs_dir[i]) %in% do::Replace0(paste0(fnj,ext),'\\\\') |>
                sum()
            len
            if (len==5) next(j)
            if (i==1){
                cat('\n     ',itemi)
            }else{
                (itemi1 <- prepare_items(nhs_dir[i-1]))
                if (itemi != itemi1){
                    if (error==1){
                        error=2
                        cat('\n     ',itemi)
                    }
                }
            }
            cat(crayon::green(paste0('\n          ',fnj,' ',len)))
        }
    }
}
yikeshu0611/nhanesR documentation built on Jan. 29, 2022, 6:08 a.m.