R/qrc_insert.R

Defines functions qrc_insert

Documented in qrc_insert

#' @title qrc_insert
#' @description Function to write dataframes to qrc_raw PostgreSQL database.
#'
#' @param table Name of table to insert to.
#' @param df Dataframe to insert.
#' @param dbhost Host IP address. Set to QRC_DB_HOST in .Renviron.
#' @param port Port that server is listening on. Defaults to 5432.
#' @param uid User ID. Set as QRC_DB_USER in .Renviron file.
#' @param pwd Password. Set as QRC_DB_PWD in .Renviron file.
#' @param ... Additional arguments such as append or overwrite.
#'
#' @export
qrc_insert <- function(
  table,
  df,
  db,
  dbhost = Sys.getenv('QRC_DB_HOST'),
  port = 5432,
  uid = Sys.getenv('QRC_DB_USER'),
  pwd = Sys.getenv('QRC_DB_PWD'),
  ...
) {
  con <- DBI::dbConnect(drv = RPostgreSQL::PostgreSQL(),
                        dbname = db,
                        host = dbhost,
                        port = port,
                        user = uid,
                        password = pwd)

  DBI::dbWriteTable(conn = con,
                    name = table,
                    value = df,
                    row.names = FALSE,
                    ...)

  DBI::dbDisconnect(conn = con)
}
kimjam/qrcutils documentation built on May 20, 2019, 10:21 p.m.