#' Create a tbl pointer to COVID_DB.DMS_COVID.
#'
#' If a name is not provided, will print a list of the available tables in COVID_DB.DMS_COVID
#'
#' @param conn a DBI-compliant connection object. recommend using the 'connect_cdw()` function
#' @param name A character string specifying a table in the DMS_COVID schema
#'
#' @return a tbl or a display of tables in the DMS_COVID schema
#'
#' @importFrom dplyr tbl
#' @importFrom dbplyr in_schema sql
#' @importFrom DBI dbListTables
#'
#' @export
#'
#' @examples
#'
#' \dontrun{
#' conn <- connect_cdw()
#'
#' covid_tbl(conn)
#'
#' covid_tbl(conn, "ENCOUNTER_LEVEL_DATAMART")
#'
#' disconnect_cdw(conn)
#' }
covid_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 DMS_COVID tables:\n\n")
results <-
DBI::dbGetQuery(conn, "SELECT * FROM COVID_DB.INFORMATION_SCHEMA.TABLES;") %>%
dplyr::filter(TABLE_SCHEMA == "DMS_COVID") %>%
dplyr::filter(TABLE_TYPE == "BASE TABLE") %>%
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("COVID_DB.DMS_COVID"), name))
#dplyr::tbl(conn, DBI::Id(database = "COVID_DB", schema = "DMS_COVID", table = name))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.