R/add_new_csv_data_to_log.R

Defines functions add_new_csv_data_to_log

Documented in add_new_csv_data_to_log

#' If any new entries are found in PROJECT_LOG.csv, it is added to the PROJECT_LOG$HISTORY and PROJECT_LOG$CURRENT is refreshed and the PROJECT_LOG object is saved
#' @export
#'
add_new_csv_data_to_log <-
        function() {

                tmp_project_log <- get("PROJECT_LOG", envir = globalenv())

                ##Getting the names of the directories
                logged_dirs <- names(tmp_project_log$HISTORY)


                ##Getting the most recent update
                x <- list()
                for (i in 1:length(tmp_project_log$HISTORY)) {
                        x[[i]] <- tail_list(tmp_project_log$HISTORY[[i]])
                }
                names(x) <- logged_dirs

                ##Getting csv data
                project_log_csv <- readr::read_csv("PROJECT_LOG.csv", col_types = cols(.default = "c")) %>%
                                        somersaulteR::mutate_all_na_to_blank()

                project_log_csv <- split(project_log_csv, project_log_csv$PROJECT_LOG_DIR)

                for (i in 1:length(logged_dirs)) {
                        logged_dir <- logged_dirs[i]

                        if (!(is.null(project_log_csv[[logged_dir]]))) {
                                new <- dplyr::setdiff(project_log_csv[[logged_dir]],
                                                      x[[logged_dir]])

                                if (nrow(new) > 0) {
                                        tmp_project_log[["HISTORY"]][[logged_dir]][[next_in_line(tmp_project_log[["HISTORY"]][[logged_dir]])]] <- bind_rows(x[[logged_dir]],
                                                                                                                                                     new)
                                        names(tmp_project_log[["HISTORY"]][[logged_dir]])[end_of_the_line(tmp_project_log[["HISTORY"]][[logged_dir]])] <- mirroR::get_timestamp()
                                        saveRDS(tmp_project_log, "PROJECT_LOG.RData")
                                        assign("PROJECT_LOG", tmp_project_log, envir = globalenv())
                                        refresh_current_project_log()
                                }
                        }
                }
        }
patelm9/projectloggeR documentation built on Nov. 4, 2019, 11:21 p.m.