R/write_project_sql_query_to_csv.R

Defines functions write_project_sql_query_to_csv

Documented in write_project_sql_query_to_csv

#' Writes csv with overwrite/append options if file exists
#' @param csv_basename basename without file extension of the output file
#' @param timestamp    TRUE if a "_YYYY-MM-DD HH:MM:SS" timestamp is desired in the output filename
#' @param path_folder  destination folder name. If this folder does not exist, it will be created.
#' @param dbname name of database that will be added to the log
#' @param schema name of schema
#' @param table name of table
#' @importFrom readr write_csv
#' @export
#'
write_project_sql_query_to_csv <-
        function(dataframe, csv_basename, timestamp = FALSE, path_folder = "SQL_QUERY_CATALOGUE", dbname = "umls", schema = "", table = "mrconso") {
                dataframe_name <- deparse(substitute(dataframe))
                mirroR::dir_create_if_not_exist(path_folder)

                if (timestamp == FALSE) {
                        fn <- mirroR::create_path_to_file(path_folder = path_folder,
                                            basename = csv_basename,
                                            file_extension = "csv")
                } else {
                        fn <- mirroR::create_path_to_file(path_folder = path_folder,
                                                  basename = paste0(csv_basename, "_", mirroR::get_timestamp()),
                                                  file_extension = "csv")
                }

                fn <- fn[1]

                if (!(file.exists(fn))) {
                                readr::write_csv(dataframe, fn)

                                typewriteR::tell_me("Finished writing ", fn, ".")

                                append_csv(paste0("./", path_folder, "/", tolower(path_folder), "_log.csv"),
                                                data.frame(LOAD_TIMESTAMP = "",
                                                           LOAD_COMMENT = "",
                                                           PARENT_FN = "",
                                                           SOURCE_COMMENT = "",
                                                           FN = "",
                                                           FN_MD5SUM = "",
                                                           LOG_ID = "",
                                                           ROBJ_NAME = dataframe_name,
                                                           RDATA_FN = "",
                                                           RDATA_FN_MD5SUM = "",
                                                           LOG_TIMESTAMP = "",
                                                           LOG_COMMENT = "",
                                                           WRITE_TIMESTAMP = mirroR::get_timestamp(),
                                                           WRITE_COMMENT = "",
                                                           WRITE_FULL_FN = fn,
                                                           WRITE_FULL_FN_MD5SUM = tools::md5sum(fn),
                                                           TO_DB_TIMESTAMP = "",
                                                           TO_DB_COMMENT = "",
                                                           TO_DB_NAME = "",
                                                           TO_DB_SCHEMA = "",
                                                           TO_DB_TABLE = "",
                                                           FROM_DB_TIMESTAMP = mirroR::get_timestamp(),
                                                           FROM_DB_COMMENT = "",
                                                           FROM_DB_NAME = dbname,
                                                           FROM_DB_SCHEMA = schema,
                                                           FROM_DB_TABLE = table)
                                           )
                }
        }
patelm9/projektoR documentation built on Dec. 18, 2019, 5:55 a.m.