R/init_table_from_df.R

Defines functions init_table_from_df

#' Init psql table from data.frame
#'
#' @param credentials list from config
#' @param db_table psql table name
#' @param df df (serves as template)
#'
#' @return
#' @export
#'
#' @examples
#' \donotrun{
#' init_table_from_df(credentials = config$get()$credentials, name = "shiny_survey", df = df_db)
#' }
init_table_from_df <- function(credentials, db_table, df) {
  
  con <- DBI::dbConnect(RPostgres::Postgres(),
                        dbname = credentials$dbname,
                        host = credentials$host,
                        user = credentials$user,
                        port = credentials$port,
                        password = credentials$password,
  )
  
  DBI::dbCreateTable(conn = con, name = db_table, fields = df)
  
  ## check
  if (!(db_table %in% DBI::dbListTables(con))) stop("Table does not exist!")
  
  ## stop connection
  DBI::dbDisconnect(con)
  
  cat("Table ", db_table, " initialized\n")
  
}
dheimgartner/shiny-deploy documentation built on Jan. 7, 2022, 12:22 a.m.