R/rEHR_functions.R

Defines functions connect_database

Documented in connect_database

#' Open connection to SQLite database
#'
#' @description
#' Open connection to SQLite database
#'
#' @param dbname Name of SQLite database on hard disk (including full file path relative to working directory)
#'
#' @returns No return value, called to open a database connection.
#'
#' @examples
#'
#' ## Connect to a database
#' aurum_extract <- connect_database(file.path(tempdir(), "temp.sqlite"))
#'
#' ## Check connection is open
#' inherits(aurum_extract, "DBIConnection")
#'
#' ## clean up
#' RSQLite::dbDisconnect(aurum_extract)
#' unlink(file.path(tempdir(), "temp.sqlite"))
#'
#' @export
connect_database <- function(dbname){
  if(!stringr::str_detect(dbname, "\\.sqlite$")) {
    dbname <- paste(dbname, "sqlite", sep = ".")
  }
  RSQLite::dbConnect(RSQLite::SQLite(), dbname)
}

Try the rcprd package in your browser

Any scripts or data that you put into this service are public.

rcprd documentation built on April 12, 2025, 1:57 a.m.