#' Create a tbl pointer to a specified schema and table in the Clinical Analytics Prod database
#'
#' If a schema name is not provided, will print a list of the available schemas in CLINICAL_ANALYTICS_PROD.
#' If a table name is not provided, will print a list of the available tables in the specified schema.
#'
#' @param conn a DBI-compliant connection object. recommend using the 'connect_cdw()` function
#' @param schema A character string specifying a table
#' @param name A character string specifying a table or view
#'
#' @return a tbl or a display of tables and views available
#
#' @importFrom dplyr tbl select mutate bind_rows
#' @importFrom dbplyr in_schema sql
#' @importFrom DBI dbListObjects
#'
#' @export
#'
#' @examples
#'
#' \dontrun{
#' conn <- connect_cdw()
#'
#' ca_prod_tbl(conn)
#'
#' ca_prod_tbl(conn, schema = "DMS_ACUTE_CARE")
#'
#' ca_prod_tbl(conn, "DMS_ACUTE_CARE", "SEPSIS")
#'
#' disconnect_cdw(conn)
#'}
ca_prod_tbl <- function(conn, schema = NA, name = NA) {
# Todo: check that there's a valid connection
if(is.na(schema)) {
cat("Schema not specified.\nHere's a list of available schemas in CLINICAL_ANALYTICS_PROD:\n\n")
results <- DBI::dbGetQuery(conn, "SHOW SCHEMAS IN DATABASE CLINICAL_ANALYTICS_PROD;") %>%
dplyr::filter(options == "MANAGED ACCESS") %>%
dplyr::select(schema = name, comment) %>%
tidyr::as_tibble()
return(results)
}
if(is.na(name)) {
paste0("Table/View not specified.\nHere's a list of available tables & views in ", schema, ":\n\n")
results <-
DBI::dbGetQuery(conn, "SELECT * FROM CLINICAL_ANALYTICS_PROD.INFORMATION_SCHEMA.TABLES;") %>%
dplyr::filter(TABLE_SCHEMA == schema) %>%
dplyr::mutate(SIZE = prettyunits::pretty_bytes(BYTES)) %>%
dplyr::mutate(COMMENT = stringr::str_trunc(COMMENT, 80)) %>%
dplyr::filter(!is.na(TABLE_OWNER)) %>%
dplyr::select(TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE,
ROW_COUNT, SIZE, LAST_ALTERED, COMMENT)
return(results)
}
# get the tbl
dplyr::tbl(conn, dbplyr::in_schema(sql(glue::glue("CLINICAL_ANALYTICS_PROD.{schema}")), name))
#dplyr::tbl(conn, DBI::Id(database = "CLINICAL_ANALYTICS", schema = schema, table = name))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.