MAY fc628cdf28 First release ready version.
Cleaned up the code, provided additional data checks and instructions on
how to fix errors.

Refactored the code so it now uses "common.R" and "VSI.R". By doing
this, all other "tools" by the WP8 team can be streamlined and made more
user friendly.
2023-01-17 10:13:25 +01:00

91 lines
2.7 KiB
R

# call this at the top of the Rmd
common_defs <- function () {
knitr::opts_chunk$set(echo = TRUE, results = "hide", message = TRUE, dev = "cairo_pdf", warning = TRUE)
knitr::opts_chunk$set(fig.pos = 'H')
options(knitr.table.format = "latex", knitr.kable.NA = "")
Sys.setlocale("LC_ALL","English")
}
# checks if file names are provided and file paths are valid.
# returns TRUE if everything's OK or FALSE if not.
check_files <- function (main, inwer) {
pass = TRUE
if (is.null(main) || main == "") {
pass = FALSE
cat ("**Main data file not provided. Please profile main data file path as 'mainFile' parameter to the script!** \n")
}
if (is.null(inwer) || inwer == "") {
pass = FALSE
cat ("**Interviewer data file not provided. Please profile interviewer data file path as 'intFile' parameter to the script!** \n")
}
if (!file.exists(main)) {
pass= FALSE
cat (paste("**I can't open the main data file '", main, "'.** \n
Did you provide a valid path to the file? \n
Please make sure that:
- it is stored on a local (not network) drive
- file name or file path does not contain any non-english characters
- file name and file path only contains characters (a-z), numbers (0-1), dash (-) or underline (_)"))
}
if (!file.exists(inwer)) {
cat (paste("**I can't open the interviewer data file '", inwer, "'.** \n
Did you provide a valid path to the file? \n
Please make sure that:
- it is stored on a local (not network) drive
- file name or file path does not contain any non-english characters
- file name and file path only contains characters (a-z), numbers (0-1), dash (-) or underline (_)"))
}
return (pass)
}
# loads the files and returns merged data
# also checks if the needed variables are present
# (check_data must be provided in additional R file, as it differs between tests)
load_files <- function (main, inwer) {
pass = TRUE
# main file
dataset <- read.csv2(main, dec=".", stringsAsFactors=F)
# exit if data is not OK
if (check_main_file(dataset) == FALSE) {
pass = FALSE
}
#interviewer file
Inwer <- read.csv2(inwer, dec=".", stringsAsFactors=F)
if (check_inwer_file(Inwer) == FALSE) {
pass = FALSE
}
# add country and interviewer ID to the main data frame; merge by idno and cntry
# only merge when files are OK
if (pass == TRUE) {
cat ("All the required variables are present, continuing with the analysis. \n")
Inwer_ID <- dplyr::select(Inwer, "intnum", "idno", "cntry")
dataset <- dplyr::left_join(dataset, Inwer_ID, by = c("idno", "cntry"))
return (dataset)
}
else {
return (FALSE)
}
}