R/cdw_tbl.R

Defines functions cdw_tbl

Documented in cdw_tbl

#' Create a tbl pointer to any CDW database/schema/table
#'
#' If a name is not provided, will print a list of the available tables in the current connection settings.
#'
#' @param conn a DBI-compliant connection object. recommend using the 'connect_cdw()` function
#' @param name 	A character string specifying the table name
#'
#' @return a tbl or a display of tables and views available
#'
#' @importFrom dplyr tbl
#' @importFrom dbplyr in_schema sql
#' @importFrom DBI dbListTables
#'
#' @export
#'
#' @examples
#'
#' \dontrun{
#' conn <- connect_cdw()
#'
#' cdw_tbl(conn)
#'
#' cdw_tbl(conn, DBI::Id(database = "CLINICAL_ANALYTICS_PROD",
#'                       schema = "DMS_ACUTE_CARE",
#'                       table = "SEPSIS"))
#'
#' disconnect_cdw(conn)
#'}


cdw_tbl <- function(conn, name = NA) {

    # Todo: check that there's a valid connection

    if(!is.na(name)) {
        dplyr::tbl(conn, name)
        #dplyr::tbl(conn, DBI::Id(database = database, schema = schema, table = name))
    }
    else {
        cat("Table not specified.\nHere's a list of available tables with the current settings:\n\n")
        results <- DBI::dbListTables(conn)

        results
    }
}
RollieParrish/ccdm documentation built on Dec. 31, 2020, 4:26 p.m.