R/clarity_tbl.R

Defines functions clarity_tbl

Documented in clarity_tbl

#' Create a tbl pointer to EpicClarity.
#'
#' If a name is not provided, will print a list of the available views in HI_DB.STG_EPICCLARITY.
#'
#' @param conn a DBI-compliant connection object. recommend using the 'connect_cdw()` function
#' @param name A character string specifying a table in the HI_DB.STG_EPICCLARITY schema
#'
#' @return  a tbl or a display of tables and views in the STG_EPICCLARITY schema
#'
#' @importFrom dplyr tbl
#' @importFrom dbplyr in_schema sql
#' @importFrom DBI dbListTables
#'
#' @export
#'
#' @examples
#'
#' \dontrun{
#' conn <- connect_cdw()
#'
#' clarity_tbl(conn)
#'
#' clarity_tbl(conn, "PAT_ENC_HOSP")
#'
#' disconnect_cdw(conn)
#' }


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

    # Todo: check that there's a valid connection
    if(is.na(name)) {
        paste0("View not specified.\nHere's a list of available Epic Clarity views:\n\n")

        results <-
            DBI::dbGetQuery(conn, "SELECT * FROM HI_DB.INFORMATION_SCHEMA.TABLES;") %>%
            dplyr::filter(TABLE_SCHEMA == "STG_EPICCLARITY") %>%
            dplyr::filter(TABLE_TYPE == "VIEW") %>%
            dplyr::mutate(SIZE = prettyunits::pretty_bytes(BYTES)) %>%
            dplyr::mutate(COMMENT = stringr::str_trunc(COMMENT, 80)) %>%
            dplyr::filter(!is.na(TABLE_OWNER)) %>%
            dplyr::select(TABLE_NAME, TABLE_TYPE, ROW_COUNT, SIZE, LAST_ALTERED, COMMENT)

        return(results)

    }

    # get the tbl
    dplyr::tbl(conn, dbplyr::in_schema(sql("HI_DB.STG_EPICCLARITY"), name))
    #dplyr::tbl(conn, DBI::Id(database = "HI_DB", schema = "STG_EPICCLARITY", table = name))

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