R/editLog.R

Defines functions editLog

Documented in editLog

#' Edit Log
#'
#' This function is used to edit the logFile created by the "createLog" function.
#' This function is also used to write the log to the desired directory, note that
#' you can "edit" and "write" the log simultaneously.  The log should only be written
#' once all of the columns are filled out.
#'
#' @param col The column in the logFile being edited.
#' @param val The value being written into the logFile column being edited.
#' @param write TRUE/FALSE for if the log should be written to the directory.
#' It will append to any previous log it finds if the format matches.
#' @param logDir The directory to which the log is written. Defaulted to working directory.
#' @return If write = TRUE then the log is written to the desired directory, else nothing.
#' @export
#'


editLog <- function(col = "", val = "", write = F, logDir = getwd()) {

  if(col != "") {

    if(val == "") {
      warning("No val provided, no log written.")
    }

    if(nrow(logFile) == 0) {

      logFile[1,] <<- NA

    }

    logFile[1, col] <<- val
  }
  if(write) {

    if(!grepl("\\/$", logDir) && !grepl("\\\\$", logDir)) {
      if(grepl("\\/", logDir)) {
        logDir <- paste0(logDir, "/")
      } else if (grepl("\\\\", logDir)) {
        logDir <- paste0(logDir, "\\")
      }
    }

    if(length(list.files(logDir, pattern = "log.csv")) == 1) {

      fLog <- read.csv(list.files(logDir, pattern = "log.csv", full.names = T),
                       stringsAsFactors = F)

      if(Reduce("&",colnames(fLog) == colnames(logFile))) {

        logFile <- rbind(fLog, logFile)
        write.csv(logFile,
                  list.files(logDir, pattern = "log.csv", full.names = T),
                  row.names = F)
        print("New log successfully added.")

      } else {
        warning("Log found in directory does not match current log, no log written.")
      }

    } else if(length(list.files(logDir, pattern = "log.csv")) == 0) {

      write.csv(logFile, paste0(logDir, "/log.csv"), row.names = F)
      print("New log file successfully created.")

    } else {

      warning("Multiple log files found in directory, no log written.")

    }

  }

}
blazickjoe/DataScienceLibrary documentation built on Nov. 5, 2019, 2:26 p.m.