readDF <- function(filepath, sht = 1, logger, skip = 0){
  tryCatch(
    {
      df <- as.data.frame(readxl::read_xlsx(path = filepath, sheet = sht, skip = skip))
      return(df)
    },
    error = function(e) {
      log4r::fatal(logger, paste0("Error in readxl::read_xlsx(): couldn't read the sheet ", sht, "."))
      log4r::fatal(logger, e)
      stop()
    },
    warning = function(w) {
      log4r::warn(logger, paste0("Warning in readxl::read_xlsx(): reading the sheet ", sht, "."))
      log4r::warn(logger, w)
    }
  )
}

writeDF <- function(filepath, resList, logger){
  tryCatch(
    {
      writexl::write_xlsx(resList, path = filepath)
    },
    error = function(e){
      log4r::fatal(logger, "Error in writexl::write_xlsx(): couldn't write the output file.")
      log4r::fatal(logger, e)
      stop()
    },
    warning = function(w){
      log4r::warn(logger, "Warning in writexl::write_xlsx(): writring the output file.")
      log4r::warn(logger, w)
    }
  )
}

turnIntoVector <- function(inpvar){
  tryCatch(
    {
      varvalue <- eval(parse(text = paste0("c(", inpvar, ")")))
      return(varvalue)
    },
    error = function(e){
      log4r::fatal(logger, "Error in eval(parse()): couldn't attribute string elements to numeric vector. Verify if elements are numbers.")
      log4r::fatal(logger, e)
      stop()
    },
    warning = function(w){
      log4r::warn(logger, "Warning in eval(parse()): trying to attribute string elements to numeric vector.")
      log4r::warn(logger, w)
    }
  )
}

multiApply <- function(FUN, ...){
  tryCatch(
    {
      return(mapply(FUN = FUN, ...))
    },
    error = function(e){
      log4r::fatal(logger, "Error in mapply(): couldn't run function.")
      log4r::fatal(logger, e)
      return(1)
    },
    warning = function(w){
      log4r::warn(logger, "Warning in mapply(): trying to run function.")
      log4r::warn(logger, w)
    }
  )
}
