R/chk_file.R

Defines functions choose_file chk_file

Documented in chk_file

#' Helper function to choose the excel file that will provide the data
#'
#' @param path (Character) the path that the excel file can be read from or null in case the user wants to have a file choose window displayed.
#' @return the path that the user chose
#'
#' @author
#' Agapios Panos <panosagapios@gmail.com>
#'
#' @importFrom tools file_ext

chk_file <- function(path){
    if (any(is.null(path), is.na(path), !is.character(path))) {
        cat('Please choose the excel file that contains the patient data.')
        path <- choose_file()
    } else if (!file.exists(path)) { # we have to check if exists after passing the check for NULL OR NA as it produces errors if we check exists(NULL) or exists(NA)
        cat('The path that you chose as excel_path seems to be invalid. Please choose a valid one.')
        path <- choose_file()
    }
    return(path)
}

choose_file <- function(){
    path <- file.choose();
    if (is.na(path))
        stop("You did not choose an xlsx file to get data from. Rerun the command and choose a file")
    if (file_ext(path) != 'xlsx' & file_ext(path) != 'xls')
        stop("You must choose an xlsx or xls file")
    return(path)
}
agapiospanos/DDI documentation built on March 11, 2021, 3:37 a.m.