R/InitialSetup.R

Defines functions InitialSetup

Documented in InitialSetup

InitialSetup <- function(addl_lib = c(), wd_path = "//AUS15JFDMN2/OPM Shared Drive/domo/", log_file_name = "") {
  if(!require(sendmailR)) { install.packages("sendmailR") }
  tryCatch( {
    log_file <- paste(paste(wd_path, ifelse(grepl(".*\\/$", wd_path, ignore.case = TRUE), "","/"), sep = ""),
                      paste(ifelse(log_file_name == "", gsub(".*\\/|\\.R", "", rstudioapi::getActiveDocumentContext()$path, ignore.case = TRUE), log_file_name), "_", rstudioapi::getActiveDocumentContext()$id, sep = ""),
                      ifelse(grepl(".*\\.log$", log_file_name, ignore.case = TRUE), "", ".log"),
                      sep = "")
    if(gsub(".*\\/|\\.R", "", rstudioapi::getActiveDocumentContext()$path, ignore.case = TRUE) == "" & log_file_name == "") {
      stop("Problem with name of file. Please rename.")
    } else if( length(list.files(path = wd_path, pattern = paste(".*", gsub(".*\\/", "", log_file, ignore.case = TRUE), ".*", sep = ""), full.names = TRUE)) == 0) {
      if( length(list.files(path = wd_path, pattern = paste(".*", gsub(".*\\_", "", log_file, ignore.case = TRUE), ".*", sep = ""), full.names = TRUE)) == 1) {
        file_to_rename <- list.files(path = wd_path, pattern = paste(".*", gsub(".*\\_", "", log_file, ignore.case = TRUE), ".*", sep = ""), full.names = TRUE)
      } else if( length(list.files(path = wd_path, pattern = paste(".*", gsub(".*\\/|\\_.*", "", log_file, ignore.case = TRUE), ".*.log.*", sep = ""), full.names = TRUE)) == 1) {
        file_to_rename <- list.files(path = wd_path, pattern = paste(".*", gsub(".*\\/|\\_.*", "", log_file, ignore.case = TRUE), ".*.log.*", sep = ""), full.names = TRUE)
      }
      file.rename(file_to_rename, log_file)
    } else if(!file.exists(log_file)) {
      file.create(log_file)
    }
    file_conn <- file(description = log_file)
  },
  error = function(err) {
    sendmail(from = "<Domo@usacompression.com>",
             to = "<Domo@usacompression.com>",
             subject = "Error Thrown In Initial Setup",
             msg = paste("Error that was thrown is listed below : \n\n\t", err, sep = ""),
             control = list(smtpServer = "email.usacompression.com"))
  },
  finally = {
    start <- grep(".*Started.*At.*", readLines(con = file_conn, n = -1L), ignore.case = TRUE)[length(grep(".*Started.*At.*", readLines(con = file_conn, n = -1L), ignore.case = TRUE))] + 1
    end <- grep(".*Ended.*At.*", readLines(con = file_conn, n = -1L), ignore.case = TRUE)[length(grep(".*Ended.*At.*", readLines(con = file_conn, n = -1L), ignore.case = TRUE))] - 1
    value <- "wt"
    if(length(grep(".*error.*", readLines(con = file_conn, n = -1L)[start:end], ignore.case = TRUE)) > 0) {
      value <- "at"
    }
    close(file_conn)
    file_conn <- file(description = log_file, open = value)
    sink(file_conn, type = "output")
    cat(paste("Started at : ", Sys.time(), "\n"))
    sink(type = "output")
    sink(file_conn, type = "message")
    addl_lib <- append(addl_lib, c("DomoR","dplyr", "stringr", "tidyr"))
    for (pckg in addl_lib) {
      if(!require(pckg, character.only = TRUE)) { install.packages(pckg) }
    }
    DomoR::init('usacompression', '840aabc7256e0e957c5675d2e015ad2e2f7530f66c7a5a06')
    return(file_conn) }
  )
}
cscheller/USACOPMStandard documentation built on Nov. 7, 2019, 12:12 a.m.