R/save_geo.R

Defines functions get_dbms geo_save

Documented in geo_save

#' Save geo codes
#'
#' Geo codes can be saved either in a database management system (DBMS) or
#' as an Excel or text file.
#'
#' @param tblname Name of the table to be saved as
#' @param obj Object name to be saved
#' @param des.path Destination folder where the file to be saved
#' @param file.type Choose file type as Access, SQLite, Excel or Text
#' @param db.name When choosing a DBMS then specify the database name
#'
#' @return No return value, called for side effects
#' @export

geo_save <- function(tblname = NULL,
                     obj = NULL,
                     des.path = FALSE,
                     file.type = c("Access", "SQLite", "Excel", "Text"),
                     db.name = NULL
                     ){

  innTyp <- tolower(file.type)

  if (innTyp  %in% c("access", "sqlite") & is.null(db.name)){
    stop("Database name is missing!")
  }


  ## DBMS
  get_dbms(db.name = db.name,
           db.path = des.path,
           tblname = tblname,
           obj = obj,
           dbms = innTyp)


  if (innTyp == "excel"){
    xlFile <- paste0(des.path, "/", tblname)
    write_tbl(obj, xlFile, innTyp)
  }

  if (innTyp == "text"){
    xlFile <- paste0(des.path, "/", tblname)
    write_tbl(obj, xlFile, innTyp)
  }


}



get_dbms <- function(db.name = NULL,
                     db.path = NULL,
                     tblname = NULL,
                     obj = NULL,
                     dbms = c("access", "sqlite")){

  dbFile <- paste(db.path, db.name, sep = "/")

  con <- switch(dbms,
                access = {
                  dbCon <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq="
                  cs <- paste0(dbCon, dbFile)
                  DBI::dbConnect(odbc::odbc(), .connection_string = cs)
                },
                sqlite = {DBI::dbConnect(RSQLite::SQLite(), dbname = dbFile)}
                )

  DBI::dbWriteTable(con, tblname, obj, batch_rows = 1, overwrite = TRUE)
  DBI::dbDisconnect(con)
}
helseprofil/norgeo documentation built on Oct. 20, 2024, 1:26 p.m.