### Create CDM reference object examples Source: https://darwin-eu.github.io/CDMConnector/reference/cdmFromCon.html Examples demonstrating how to initialize a CDM reference object with different schema and catalog configurations. ```R if (FALSE) { # \dontrun{ library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) # minimal example cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "scratch") # write prefix is optional but recommended if write_schema is shared cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "scratch", writePrefix = "tmp_") # Some database systems use catalogs or compound schemas. # These can be specified as follows: cdm <- cdmFromCon(con, cdmSchema = "catalog.main", writeSchema = "catalog.scratch", writePrefix = "tmp_") cdm <- cdmFromCon(con, cdmSchema = c("my_catalog", "main"), writeSchema = c("my_catalog", "scratch"), writePrefix = "tmp_") cdm <- cdmFromCon(con, cdmSchema = c(catalog = "my_catalog", schema = "main"), writeSchema = c(catalog = "my_catalog", schema = "scratch"), writePrefix = "tmp_") DBI::dbDisconnect(con) } # } ``` -------------------------------- ### List Available Example Datasets Source: https://darwin-eu.github.io/CDMConnector/articles/a01_getting-started.html Use `exampleDatasets()` to view a list of available synthetic datasets for testing and examples. Ensure the `CDMConnector` library is loaded. ```r library(CDMConnector) exampleDatasets() ``` -------------------------------- ### Install CDMConnector Source: https://darwin-eu.github.io/CDMConnector/index.html Installation instructions for the package from CRAN or GitHub. ```R install.packages("CDMConnector") ``` ```R # install.packages("devtools") devtools::install_github("darwin-eu/CDMConnector") ``` -------------------------------- ### Example: Connecting to DuckDB and Listing Tables Source: https://darwin-eu.github.io/CDMConnector/reference/listTables.html Demonstrates how to establish a DBI connection to a DuckDB database and then list tables within the 'main' schema using the listTables function. This example is wrapped in \"dontrun\" to prevent accidental execution. ```R if (FALSE) { # \dontrun{ con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) listTables(con, schema = "main") } # } ``` -------------------------------- ### Example: Generating a Cohort Set Source: https://darwin-eu.github.io/CDMConnector/reference/generateCohortSet2.html A complete example demonstrating how to connect to a CDM, read a cohort set, and generate the cohort table using generateCohortSet2. ```R if (FALSE) { # \dontrun{ library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") cohortSet <- readCohortSet(system.file("cohorts2", package = "CDMConnector")) cdm <- generateCohortSet2(cdm, cohortSet, name = "cohort") print(cdm$cohort) attrition(cdm$cohort) settings(cdm$cohort) cohortCount(cdm$cohort) } # } ``` -------------------------------- ### computeQuery Examples Source: https://darwin-eu.github.io/CDMConnector/reference/computeQuery.html Demonstrates creating both temporary and permanent tables in a remote database using CDMConnector. ```R if (FALSE) { # \dontrun{ library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con, "main") # create a temporary table in the remote database from a dplyr query drugCount <- cdm$concept %>% dplyr::count(domain_id == "Drug") %>% computeQuery() # create a permanent table in the remote database from a dplyr query drugCount <- cdm$concept %>% dplyr::count(domain_id == "Drug") %>% computeQuery("tmp_table", temporary = FALSE, schema = "main") DBI::dbDisconnect(con, shutdown = TRUE) } # } ``` -------------------------------- ### Download Eunomia Data Example Source: https://darwin-eu.github.io/CDMConnector/reference/downloadEunomiaData.html Demonstrates a basic call to download the GiBleed dataset. ```R if (FALSE) { # \dontrun{ downloadEunomiaData("GiBleed") } # } ``` -------------------------------- ### List Available Example CDM Datasets Source: https://darwin-eu.github.io/CDMConnector/reference/exampleDatasets.html Call this function to get a list of available example CDM dataset identifiers. Requires the CDMConnector package. ```R exampleDatasets() ``` ```R if (FALSE) { # \dontrun{ library(CDMConnector) exampleDatasets()[1] #> [1] "GiBleed" con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("GiBleed")) cdm <- cdmFromCon(con) } # } ``` -------------------------------- ### Generate an Example Cohort Set Source: https://darwin-eu.github.io/CDMConnector/articles/a02_cohorts.html This code generates an example cohort set using CDMConnector. It requires establishing a database connection and loading cohort definitions. ```r library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") cohortSet <- readCohortSet(system.file("cohorts3", package = "CDMConnector")) cdm <- generateCohortSet(cdm, cohortSet, name = "cohort") #> ℹ️ Generating 5 cohorts #> ℹ️ Generating cohort (1/5) - gibleed_all_end_10 #> ✅ Generating cohort (1/5) - gibleed_all_end_10 [63ms] #> #> ℹ️ Generating cohort (2/5) - gibleed_all #> ✅ Generating cohort (2/5) - gibleed_all [54ms] #> #> ℹ️ Generating cohort (3/5) - gibleed_default_with_descendants #> ✅ Generating cohort (3/5) - gibleed_default_with_descendants [55ms] #> #> ℹ️ Generating cohort (4/5) - gibleed_default #> ✅ Generating cohort (4/5) - gibleed_default [59ms] #> #> ℹ️ Generating cohort (5/5) - gibleed_end_10 #> ✅ Generating cohort (5/5) - gibleed_end_10 [55ms] #> cdm$cohort #> # Source: table [?? x 4] #> # Database: DuckDB 1.4.4 [root@Darwin 25.3.0:R 4.5.1//private/var/folders/2j/8z0yfn1j69q8sxjc7vj9yhz40000gp/T/RtmpAWGYkM/file1085027e0f52a.duckdb] #> cohort_definition_id subject_id cohort_start_date cohort_end_date #> #> 1 1 693 1995-09-01 1995-09-11 #> 2 1 1525 1998-12-22 1999-01-01 #> 3 1 2103 2010-11-02 2010-11-12 #> 4 1 2485 1953-04-30 1953-05-10 #> 5 1 3216 1995-12-22 1996-01-01 #> 6 1 4340 2010-04-28 2010-05-08 #> 7 1 5142 1985-05-16 1985-05-26 #> 8 1 5195 1995-07-26 1995-08-05 #> 9 1 3283 2016-07-03 2016-07-13 #> 10 1 893 1993-11-09 1993-11-19 #> # ℹ more rows cohortCount(cdm$cohort) #> # A tibble: 5  3 #> cohort_definition_id number_records number_subjects #> #> 1 1 479 479 #> 2 2 479 479 #> 3 3 479 479 #> 4 4 479 479 #> 5 5 479 479 ``` -------------------------------- ### Example Usage of cdmFromCohortSet Source: https://darwin-eu.github.io/CDMConnector/reference/cdmFromCohortSet.html Demonstrates how to read a cohort set and generate a synthetic CDM reference. ```R if (FALSE) { # \dontrun{ library(CDMConnector) cohortSet <- readCohortSet(system.file("cohorts", package = "CDMConnector")) cdm <- cdmFromCohortSet(cohortSet, n = 100) cdm$person } # } ``` -------------------------------- ### Example: Appending to a Database Table Source: https://darwin-eu.github.io/CDMConnector/reference/appendPermanent.html Demonstrates creating an initial table with compute and subsequently appending new query results using appendPermanent. ```R if (FALSE) { # \dontrun{ library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) concept <- dplyr::tbl(con, "concept") # create a table rxnorm_count <- concept %>% dplyr::filter(domain_id == "Drug") %>% dplyr::mutate(isRxnorm = (vocabulary_id == "RxNorm")) %>% dplyr::count(domain_id, isRxnorm) %>% compute("rxnorm_count") # append to an existing table rxnorm_count <- concept %>% dplyr::filter(domain_id == "Procedure") %>% dplyr::mutate(isRxnorm = (vocabulary_id == "RxNorm")) %>% dplyr::count(domain_id, isRxnorm) %>% appendPermanent("rxnorm_count") DBI::dbDisconnect(con, shutdown = TRUE) } # } ``` -------------------------------- ### List Example CDM Datasets Source: https://darwin-eu.github.io/CDMConnector/reference/exampleDatasets.html This function lists the identifiers of the available example CDM datasets that can be used with the CDMConnector package. ```APIDOC ## exampleDatasets ### Description List the available example CDM datasets ### Usage ```R exampleDatasets() ``` ### Value A character vector with example CDM dataset identifiers ### Examples ```R if (FALSE) { # \dontrun{ library(CDMConnector) exampleDatasets()[1] #> [1] "GiBleed" con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("GiBleed")) cdm <- cdmFromCon(con) } # } ``` ``` -------------------------------- ### Example usage of generateCohortSet Source: https://darwin-eu.github.io/CDMConnector/reference/generateCohortSet.html Demonstrates connecting to a database, reading a cohort set, and generating the cohort table with associated attributes. ```R if (FALSE) { # \dontrun{ library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") cohortSet <- readCohortSet(system.file("cohorts2", package = "CDMConnector")) cdm <- generateCohortSet(cdm, cohortSet, name = "cohort") print(cdm$cohort) attrition(cdm$cohort) settings(cdm$cohort) cohortCount(cdm$cohort) } # } ``` -------------------------------- ### Query COHD for similar concepts Source: https://darwin-eu.github.io/CDMConnector/reference/cohdSimilarConcepts.html Examples demonstrating how to find similar concepts for a single OMOP ID or a set of multiple IDs. ```R if (FALSE) { # \dontrun{ # Single concept: top 25 similar to concept 201826 (Type 2 diabetes) cohdSimilarConcepts(201826, datasetId = 1, topN = 25) # Multiple concepts: concepts likely to co-occur with this set cohdSimilarConcepts(c(201826, 316866, 255573), datasetId = 1, topN = 50) } # } ``` -------------------------------- ### Create Eunomia Dataset Copy Source: https://darwin-eu.github.io/CDMConnector/reference/eunomiaDir.html Creates a copy of an example OMOP CDM dataset from Eunomia. Requires EUNOMIA_DATA_FOLDER environment variable to be set for downloading datasets. Use for testing and development. ```R eunomiaDir( datasetName = "GiBleed", cdmVersion = "5.3", databaseFile = tempfile(fileext = ".duckdb") ) ``` -------------------------------- ### Example Usage of cdmCommentContents Source: https://darwin-eu.github.io/CDMConnector/reference/cdmCommentContents.html Demonstrates connecting to a CDM and using cdmCommentContents to output patient data as comments. Requires an interactive RStudio session. ```R if (FALSE) { # \dontrun{ library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, "main", "main") cdmCommentContents(cdm, personIds = 6) # person_id | observation_concept_id | start_date | end_date | type_concept_id... # 6 | 40213296 | 2006-01-10 | 2006-01-10 | 581452 ... # 6 | 40213227 | 2006-01-10 | 2006-01-10 | 581452 ... # 6 | 1118084 | 2005-07-13 | 2005-07-13 | 38000177 ... # 6 | 80180 | 2005-07-13 | NA | 32020 ... cdmDisconnect(cdm) } # } ``` -------------------------------- ### Connect to SQL Server via ODBC Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html Establishes a connection to SQL Server using the odbc package. Requires the ODBC Driver for SQL Server to be installed. ```R con <- DBI::dbConnect(odbc::odbc(), Driver = "ODBC Driver 18 for SQL Server", Server = Sys.getenv("CDM5_SQL_SERVER_SERVER"), Database = Sys.getenv("CDM5_SQL_SERVER_CDM_DATABASE"), UID = Sys.getenv("CDM5_SQL_SERVER_USER"), PWD = Sys.getenv("CDM5_SQL_SERVER_PASSWORD"), TrustServerCertificate="yes", Port = 1433) cdm <- cdmFromCon(con, cdmSchema = c("cdmv54", "dbo"), writeSchema = c("tempdb", "dbo")) DBI::dbDisconnect(con) ``` ```R con <- DBI::dbConnect(odbc::odbc(), "SQL") cdm <- cdmFromCon(con, cdmSchema = c("tempdb", "dbo"), writeSchema = c("ATLAS", "RESULTS")) DBI::dbDisconnect(con) ``` -------------------------------- ### Connect to DuckDB using duckdb package Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html Use this method to connect to a DuckDB database when you have the `duckdb` package installed. The `dbdir` argument specifies the location of the database file. ```r library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir("GiBleed")) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") DBI::dbDisconnect(con) ``` -------------------------------- ### Connect to Redshift using DatabaseConnector Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html Establish a connection to Redshift using the DatabaseConnector package (version 7+). This example utilizes createConnectionDetails for setting up connection parameters. ```r library(DatabaseConnector) connectionDetails <- createConnectionDetails(dbms = "redshift", server = Sys.getenv("CDM5_REDSHIFT_SERVER"), user = Sys.getenv("CDM5_REDSHIFT_USER"), password = Sys.getenv("CDM5_REDSHIFT_PASSWORD"), port = Sys.getenv("CDM5_REDSHIFT_PORT")) con <- connect(connectionDetails) cdm <- cdmFromCon(con, cdmSchema = Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA"), writeSchema = Sys.getenv("CDM5_REDSHIFT_SCRATCH_SCHEMA")) disconnect(con) ``` -------------------------------- ### Example of subsetting a CDM reference Source: https://darwin-eu.github.io/CDMConnector/reference/cdmSubsetCohort.html Demonstrates connecting to a database, generating a cohort, and subsetting the CDM reference to include only individuals from that cohort. ```R if (FALSE) { # \dontrun{ library(CDMConnector) library(dplyr, warn.conflicts = FALSE) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") # generate a cohort path <- system.file("cohorts2", mustWork = TRUE, package = "CDMConnector") cohortSet <- readCohortSet(path) %>% filter(cohort_name == "GIBleed_male") # subset cdm to persons in the generated cohort cdm <- generateCohortSet(cdm, cohortSet = cohortSet, name = "gibleed") cdmGiBleed <- cdmSubsetCohort(cdm, cohortTable = "gibleed") cdmGiBleed$person %>% tally() #> # Source: SQL [1 x 1] #> # Database: DuckDB 0.6.1 #> n #> #> 1 237 cdm$person %>% tally() #> # Source: SQL [1 x 1] #> # Database: DuckDB 0.6.1 #> n #> #> 1 2694 DBI::dbDisconnect(con, shutdown = TRUE) } # } ``` -------------------------------- ### Run All Package Tests Source: https://darwin-eu.github.io/CDMConnector/CONTRIBUTING.html Execute all package tests to ensure code integrity. Make sure all required packages from imports and suggests are installed. ```r devtools::test() ``` -------------------------------- ### Select CDM table groups using tblGroup Source: https://darwin-eu.github.io/CDMConnector/reference/tblGroup.html Use `tblGroup` to specify which groups of CDM tables to include in your analysis. This example demonstrates connecting to a database, creating a CDM reference, and then selecting tables belonging to the 'vocab' group. ```r if (FALSE) { # \dontrun{ con <- DBI::dbConnect(RPostgres::Postgres(), dbname = "cdm", host = "localhost", user = "postgres", password = Sys.getenv("PASSWORD")) cdm <- cdmFromCon(con, cdmName = "test", cdmSchema = "public") %>% cdmSelectTbl(tblGroup("vocab")) } # } ``` -------------------------------- ### Get DBMS from CDM Reference or DBI Connection Source: https://darwin-eu.github.io/CDMConnector/reference/dbms.html Retrieves the database management system (DBMS) from a cdm_reference or DBI connection. Ensure a DBI connection is established and a cdm_reference is created if needed. ```R if (FALSE) { # \dontrun{ con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con) dbms(cdm) dbms(con) } # } ``` -------------------------------- ### Access CDM Tables via Reference Source: https://darwin-eu.github.io/CDMConnector/articles/a01_getting-started.html Once a CDM reference object (`cdm`) is created, individual OMOP CDM tables can be accessed using `$` notation, for example, `cdm$observation_period`. ```r cdm$observation_period ``` -------------------------------- ### Load required packages Source: https://darwin-eu.github.io/CDMConnector/articles/a03_dbplyr.html Initializes the environment by loading CDMConnector, dplyr, and ggplot2. ```R library(CDMConnector) library(dplyr, warn.conflicts = FALSE) #> Warning: package 'dplyr' was built under R version 4.5.2 library(ggplot2) #> Warning: package 'ggplot2' was built under R version 4.5.2 ``` -------------------------------- ### GET version Source: https://darwin-eu.github.io/CDMConnector/reference/version.html Retrieves the CDM version attribute from a cdm_reference object. ```APIDOC ## version(cdm) ### Description Extract the CDM version attribute from a cdm_reference object. ### Parameters #### Arguments - **cdm** (cdm object) - Required - A cdm object ### Value Returns a string representing the CDM version, such as "5.3" or "5.4". ### Request Example ```r library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") version(cdm) ``` ``` -------------------------------- ### Update and Check Package Documentation Source: https://darwin-eu.github.io/CDMConnector/CONTRIBUTING.html Run these commands to update and check package documentation. Ensure `devtools::check_man()` returns no warnings. ```r devtools::document() devtools::run_examples() devtools::build_readme() devtools::build_vignettes() devtools::check_man() ``` -------------------------------- ### Connect to Eunomia Dataset Source: https://darwin-eu.github.io/CDMConnector/articles/a06_using_cdm_attributes.html Establishes a connection to the Eunomia dataset using duckdb and initializes a CDM reference. Ensure necessary packages are loaded. ```R library(CDMConnector) library(omopgenerics) library(dplyr) write_schema <- "main" cdm_schema <- "main" con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con, cdmName = "eunomia", cdmSchema = cdm_schema, writeSchema = write_schema, cdmVersion = "5.3") ``` -------------------------------- ### Get underlying database connection Source: https://darwin-eu.github.io/CDMConnector/reference/cdmCon.html Retrieves the underlying database connection from a CDM reference object. ```APIDOC ## Get underlying database connection ### Description Retrieves the underlying database connection from a CDM reference object. ### Method `cdmCon(cdm)` ### Arguments - **cdm** (object) - A cdm reference object created by `cdmFromCon`. ### Value A reference to the database containing tables in the cdm reference. ### Examples ```R if (FALSE) { # \dontrun{ con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con = con, cdmName = "Eunomia", cdmSchema = "main", writeSchema = "main") cdmCon(cdm) DBI::dbDisconnect(con) } # } ``` ``` -------------------------------- ### Create a CDM database source in R Source: https://darwin-eu.github.io/CDMConnector/reference/dbSource.html Initializes a CDM source using a database connection and an optional write schema. ```R dbSource(con, writeSchema) ``` -------------------------------- ### eunomiaDir Source: https://darwin-eu.github.io/CDMConnector/reference/eunomiaDir.html Creates a copy of a Eunomia database in duckdb and returns the path to the new database file. ```APIDOC ## eunomiaDir ### Description Creates a copy of a Eunomia database in duckdb and returns the path to the new database file. If the dataset does not exist locally, it attempts to download the source data to the path defined by the EUNOMIA_DATA_FOLDER environment variable. ### Parameters #### Arguments - **datasetName** (string) - Optional - The name of the dataset to copy. Defaults to "GiBleed". - **cdmVersion** (string) - Optional - The OMOP CDM version. Must be "5.3" or "5.4". - **databaseFile** (string) - Optional - The full path to the new copy of the example CDM dataset. Defaults to a temporary file. ### Value - The file path to the new Eunomia dataset copy (string) ### Request Example ```r eunomiaDir(datasetName = "GiBleed", cdmVersion = "5.3") ``` ``` -------------------------------- ### Get CDM Write Schema Source: https://darwin-eu.github.io/CDMConnector/reference/cdmWriteSchema.html Retrieves the database write schema associated with a CDM reference object. ```APIDOC ## GET /cdmWriteSchema ### Description Retrieves the database write schema from a CDM reference object. ### Method GET ### Endpoint /cdmWriteSchema ### Parameters #### Query Parameters - **cdm** (object) - Required - A CDM reference object created by `cdmFromCon`. ### Response #### Success Response (200) - **schema** (string) - The database write schema. ### Response Example ```json { "schema": "main" } ``` ``` -------------------------------- ### Get CDM Version Source: https://darwin-eu.github.io/CDMConnector/reference/version.html Extracts the CDM version attribute from a cdm_reference object. Requires the CDMConnector library and a database connection. ```R if (FALSE) { # \dontrun{ library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") version(cdm) DBI::dbDisconnect(con, shutdown = TRUE) } # } ``` -------------------------------- ### Connect to DuckDB using DatabaseConnector package Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html This method uses the `DatabaseConnector` package to establish a connection to DuckDB. The `server` argument in `createConnectionDetails` should point to the DuckDB file location. ```r library(DatabaseConnector) connectionDetails <- createConnectionDetails( "duckdb", server = CDMConnector::eunomiaDir("GiBleed")) con <- connect(connectionDetails) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") disconnect(con) ``` -------------------------------- ### GET dbms Source: https://darwin-eu.github.io/CDMConnector/reference/dbms.html Retrieves the database management system (dbms) string from a DBI connection or cdm_reference object, which is compatible with SqlRender. ```APIDOC ## dbms(con) ### Description Get the database management system (dbms) from a cdm_reference or DBI connection. ### Arguments - **con** (DBI connection or cdm_reference) - Required - A DBI connection or cdm_reference object. ### Value A character string representing the dbms that can be used with SqlRender. ### Request Example ```r con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con) dbms(cdm) dbms(con) ``` ``` -------------------------------- ### Get CDM Version Source: https://darwin-eu.github.io/CDMConnector/articles/a06_using_cdm_attributes.html Retrieves the version of the OMOP CDM that the reference is connected to. This helps in understanding the data structure and compatibility. ```R cdmVersion(cdm) #> [1] "5.3" ``` -------------------------------- ### GET /association/relativeFrequency Source: https://darwin-eu.github.io/CDMConnector/reference/cohdSimilarConcepts.html Queries the COHD API to retrieve concepts that co-occur with the provided input concept(s), ranked by relative frequency. ```APIDOC ## GET /association/relativeFrequency ### Description Queries the COHD API association/relativeFrequency endpoint to return concepts that co-occur with the given concept(s), ranked by relative frequency. Useful for finding clinically related conditions, drugs, or procedures based on EHR prevalence. ### Method GET ### Endpoint https://cohd-api.ci.transltr.io/api/association/relativeFrequency ### Parameters #### Query Parameters - **conceptId** (Integer/Character vector) - Required - One or more OMOP concept IDs to find similar concepts for. - **datasetId** (Integer) - Optional - COHD dataset ID (1 = 5-year, 2 = lifetime; default 1). - **topN** (Integer) - Optional - Maximum number of similar concepts to return (default 50). - **timeoutSec** (Numeric) - Optional - Request timeout in seconds (default 30). - **baseUrl** (Character) - Optional - Base URL of the COHD API. ### Response #### Success Response (200) - **Single concept result** (Data frame) - Contains concept_id_1, concept_id_2, concept_count_1, concept_count_2, concept_count, relative_frequency, and other_concept_id. - **Multiple concepts result** (Data frame) - Contains other_concept_id, n_concepts, and mean_rf. ``` -------------------------------- ### GET /cohdSimilarConcepts Source: https://darwin-eu.github.io/CDMConnector/reference/index.html Find concepts that co-occur with given OMOP concepts using the Columbia Open Health Data (COHD) API. ```APIDOC ## GET /cohdSimilarConcepts ### Description Find concepts that co-occur with given OMOP concepts using the Columbia Open Health Data (COHD) API. ### Method GET ### Endpoint /cohdSimilarConcepts ``` -------------------------------- ### Get Cache Statistics Source: https://darwin-eu.github.io/CDMConnector/articles/a08_dag-caching.html Retrieves summary statistics for the DAG cache, including the total number of entries and a breakdown by entry type. ```r dag_cache_stats(con, schema = "results") ``` -------------------------------- ### Clear Entire Cache Source: https://darwin-eu.github.io/CDMConnector/articles/a08_dag-caching.html Removes all cached tables and associated registry entries from the DAG cache, effectively starting the cache fresh. ```r dag_cache_clear(con, schema = "results") ``` -------------------------------- ### Generate Cohort SQL via Single-Cohort and Batch Paths Source: https://darwin-eu.github.io/CDMConnector/articles/a07_batch-optimization.html Demonstrates how the same internal builder function is utilized for both single-cohort and batch-optimized SQL generation. ```R # Single-cohort path: result <- build_cohort_query_internal(cohort, opts) sql <- result$full_sql # Batch path (same builder, structured output): result <- build_cohort_query_internal(cohort, opts) ``` -------------------------------- ### Connect to a DuckDB OMOP CDM Database Source: https://darwin-eu.github.io/CDMConnector/articles/a01_getting-started.html Connect to a DuckDB database containing OMOP CDM data using `DBI::dbConnect`. The `eunomiaDir("GiBleed")` function provides a path to a sample dataset. After connecting, `DBI::dbListTables(con)` can be used to inspect the available tables. ```r con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("GiBleed")) DBI::dbListTables(con) ``` -------------------------------- ### Create a CDM Reference Source: https://darwin-eu.github.io/CDMConnector/index.html Initialize a cdm_reference object from a DBI connection by specifying the CDM and write schemas. ```R library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(dbdir = eunomiaDir())) cdm <- cdmFromCon(con = con, cdmSchema = "main", writeSchema = "main", cdmName = "my_duckdb_database") ``` -------------------------------- ### Connect to GiBleed Dataset Source: https://darwin-eu.github.io/CDMConnector/reference/eunomiaDir.html Connects to the default GiBleed Eunomia dataset using duckdb. Assumes the dataset is already available or will be downloaded. ```R library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, "main", "main") cdmDisconnect(cdm) ``` -------------------------------- ### Get Cohort Attrition Source: https://darwin-eu.github.io/CDMConnector/articles/a06_using_cdm_attributes.html Retrieves the attrition summary for cohorts generated by `generateConceptCohortSet`. This provides insights into the steps and reasons for cohort exclusion during generation. ```r attrition(cdm$study_cohorts) ``` -------------------------------- ### Create a CDM reference Source: https://darwin-eu.github.io/CDMConnector/articles/a03_dbplyr.html Establishes a connection to a DuckDB database containing Eunomia data and creates a CDM reference object. ```R con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con, cdmName = "eunomia", cdmSchema = "main", writeSchema = "main") cdm ``` -------------------------------- ### Get Cohort Counts Source: https://darwin-eu.github.io/CDMConnector/articles/a06_using_cdm_attributes.html Retrieves the counts of records and subjects for each cohort within a cohort set. This function is essential for understanding the size of each cohort. ```r cohortCount(cdm$study_cohorts) ``` -------------------------------- ### List Cohort JSON Files Source: https://darwin-eu.github.io/CDMConnector/articles/a02_cohorts.html Lists the cohort JSON files available in a specified package directory. This is a preliminary step before reading cohort definitions. ```r pathToCohortJsonFiles <- system.file("cohorts1", package = "CDMConnector") list.files(pathToCohortJsonFiles) #> [1] "cerebral_venous_sinus_thrombosis_01.json" #> [2] "CohortsToCreate.csv" #> [3] "deep_vein_thrombosis_01.json" ``` -------------------------------- ### Get CDM Name Source: https://darwin-eu.github.io/CDMConnector/articles/a06_using_cdm_attributes.html Retrieves the name associated with the CDM reference. This is useful for identifying specific CDM instances, especially in network studies. ```R cdmName(cdm) #> [1] "eunomia" ``` -------------------------------- ### Create a CDM Reference Object Source: https://darwin-eu.github.io/CDMConnector/articles/a01_getting-started.html Create a CDM reference object using `cdmFromCon`. This requires an active database connection (`con`), a unique name for the CDM (`cdmName`), the schema containing OMOP tables (`cdmSchema`), and a schema for writing temporary tables (`writeSchema`). ```r cdm <- cdmFromCon(con, cdmName = "eunomia", cdmSchema = "main", writeSchema = "main") ``` -------------------------------- ### Get Underlying Database Connection Source: https://darwin-eu.github.io/CDMConnector/reference/cdmCon.html Retrieve the database connection object from a CDM reference. Ensure the CDM reference is created using `cdmFromCon`. ```R con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con = con, cdmName = "Eunomia", cdmSchema = "main", writeSchema = "main") cdmCon(cdm) DBI::dbDisconnect(con) ``` -------------------------------- ### Run Single Database Benchmark Source: https://darwin-eu.github.io/CDMConnector/articles/a09_benchmarking.html Execute the benchmark for a single CDM connection. This includes generating cohorts using both methods and comparing the resulting tables for identical rows. ```R source("extras/benchmark_cohort_generation.R") cohort_set <- CDMConnector::readCohortSet("path/to/cohorts") result <- benchmark_cohort_generation(cdm, cohort_set, cohort_path = "path/to/cohorts") # Compare old vs new cohort tables (identical rows, order ignored) cmp <- compare_cohort_tables(result$cdm, name_old = "cohort_bench_old", name_new = "cohort_bench_new") cmp$identical # TRUE if same set of rows cmp$per_cohort # Per-cohort row counts and match status ``` -------------------------------- ### Connect to Redshift using RPostgres Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html Connect to a Redshift database using the RPostgres package. Ensure all necessary environment variables for Redshift connection are configured. ```r con <- DBI::dbConnect(RPostgres::Redshift(), dbname = Sys.getenv("CDM5_REDSHIFT_DBNAME"), host = Sys.getenv("CDM5_REDSHIFT_HOST"), port = Sys.getenv("CDM5_REDSHIFT_PORT"), user = Sys.getenv("CDM5_REDSHIFT_USER"), password = Sys.getenv("CDM5_REDSHIFT_PASSWORD")) cdm <- cdmFromCon(con, cdmSchema = Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA"), writeSchema = Sys.getenv("CDM5_REDSHIFT_SCRATCH_SCHEMA")) DBI::dbDisconnect(con) ``` -------------------------------- ### Run Package Check Source: https://darwin-eu.github.io/CDMConnector/CONTRIBUTING.html Perform a comprehensive check of the package before opening a pull request. No warnings should be observed. ```r devtools::check() ``` -------------------------------- ### Run Multi-Database Benchmark Source: https://darwin-eu.github.io/CDMConnector/articles/a09_benchmarking.html Benchmark cohort generation across multiple database platforms simultaneously. Requires a named list of CDM connections and specifies output file paths for results and equivalence checks. ```R source("extras/benchmark_cohort_generation.R") source("extras/benchmark_multi_database.R") cohort_set <- CDMConnector::readCohortSet("path/to/cohorts") cdms <- list( postgres = cdm_postgres, redshift = cdm_redshift, snowflake = cdm_snowflake, spark = cdm_spark, sql_server = cdm_sqlserver ) run_benchmark_multi_database( cdms = cdms, cohort_set = cohort_set, cohort_path = "path/to/cohorts", results_csv = "benchmark_results.csv", equivalence_csv = "benchmark_equivalence.csv" ) ``` -------------------------------- ### Connect to Postgres using DatabaseConnector Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html Connect to PostgreSQL using DatabaseConnector version 7 or later. This method requires the DatabaseConnector package and uses createConnectionDetails for configuration. ```r library(DatabaseConnector) connectionDetails <- createConnectionDetails(dbms = "postgresql", server = Sys.getenv("CDM5_POSTGRESQL_SERVER"), user = Sys.getenv("CDM5_POSTGRESQL_USER"), password = Sys.getenv("CDM5_POSTGRESQL_PASSWORD")) con <- connect(connectionDetails) cdm <- cdmFromCon(con, cdmSchema = Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA"), writeSchema = Sys.getenv("CDM5_POSTGRESQL_SCRATCH_SCHEMA")) disconnect(con) ``` -------------------------------- ### Insert and Convert Table to CohortTable Source: https://darwin-eu.github.io/CDMConnector/articles/a02_cohorts.html Insert a manually created cohort table into the CDM database using `insertTable` and then convert it into a `CohortTable` object using `newCohortTable` for further analysis. ```r library(omopgenerics) #> Warning: package 'omopgenerics' was built under R version 4.5.2 #> #> Attaching package: 'omopgenerics' #> The following object is masked from 'package:stats': #> #> filter cdm <- insertTable(cdm = cdm, name = "cohort", table = cohort, overwrite = TRUE) cdm$cohort #> # Source: table [?? x 4] #> # Database: DuckDB 1.4.4 [root@Darwin 25.3.0:R 4.5.1//private/var/folders/2j/8z0yfn1j69q8sxjc7vj9yhz40000gp/T/RtmpAWGYkM/file1085027e0f52a.duckdb] #> cohort_definition_id subject_id cohort_start_date cohort_end_date #> #> 1 1 1 1999-01-01 2001-01-01 ``` ```r cdm$cohort <- newCohortTable(cdm$cohort) ``` -------------------------------- ### Automated Cohort Set Equivalence Validation Source: https://darwin-eu.github.io/CDMConnector/articles/a07_batch-optimization.html This R code outlines the process for validating the equivalence of cohort sets generated by different paths (batch vs. individual). It involves running both paths and comparing the results row-by-row for matching subject IDs, cohort start dates, and cohort end dates. ```r # Run batch path cdm <- generateCohortSet2(cdm, cohortSet, name = "batch_test") # Run each cohort independently (optimize=FALSE) for (each cohort) { atlas_json_to_sql_batch(single_cohort, optimize = FALSE) } # Compare per cohort_definition_id: # (subject_id, cohort_start_date, cohort_end_date) must match exactly ``` -------------------------------- ### Download Eunomia Data Files Source: https://darwin-eu.github.io/CDMConnector/reference/downloadEunomiaData.html This function downloads the Eunomia data files from a specified GitHub repository. ```APIDOC ## Download Eunomia data files ### Description Downloads the Eunomia data files from https://github.com/darwin-eu/EunomiaDatasets. ### Method Function Call ### Endpoint N/A (R function) ### Parameters #### Arguments - **datasetName** (string) - Required - The data set name as found on https://github.com/darwin-eu/EunomiaDatasets. The data set name corresponds to the folder with the data set ZIP files. - **cdmVersion** (string) - Optional - The OMOP CDM version. Must be '5.3' (default) or '5.4'. - **pathToData** (string) - Optional - The path where the Eunomia data is stored on the file system. By default, the value of the environment variable "EUNOMIA_DATA_FOLDER" is used. - **overwrite** (boolean) - Optional - Control whether the existing archive file will be overwritten should it already exist. ### Request Example ```R downloadEunomiaData( datasetName = "GiBleed", cdmVersion = "5.3", pathToData = Sys.getenv("EUNOMIA_DATA_FOLDER"), overwrite = FALSE ) ``` ### Response #### Success Response Invisibly returns the destination if the download was successful. #### Response Example ```R "/path/to/downloaded/data" ``` ``` -------------------------------- ### Incremental Cohort Set Updates with Caching Source: https://darwin-eu.github.io/CDMConnector/articles/a08_dag-caching.html Demonstrates how to use caching with `generateCohortSet2` for incremental updates. Caching reuses previously computed nodes, reducing computation time when cohort definitions change or new cohorts are added. Ensure `cache = TRUE` is set for caching to be active. ```R cohortSet_v1 <- data.frame( cohort_definition_id = c(1, 2, 3), cohort = c(json_diabetes, json_hypertension, json_ckd) ) cdm <- generateCohortSet2(cdm, cohortSet_v1, "cohorts", cache = TRUE) #> DAG cache: 0 hits, 18 misses (18 nodes to compute) cohortSet_v2 <- data.frame( cohort_definition_id = c(1, 2, 3), cohort = c(json_diabetes, json_hypertension, json_ckd_v2) ) cdm <- generateCohortSet2(cdm, cohortSet_v2, "cohorts", cache = TRUE) #> DAG cache: 12 hits, 6 misses (6 nodes to compute) cohortSet_v3 <- data.frame( cohort_definition_id = c(1, 2, 3, 4), cohort = c(json_diabetes, json_hypertension, json_ckd_v2, json_stroke) ) cdm <- generateCohortSet2(cdm, cohortSet_v3, "cohorts", cache = TRUE) #> DAG cache: 18 hits, 6 misses (6 nodes to compute) ``` -------------------------------- ### Connect to SQL Server via DatabaseConnector Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html Uses the DatabaseConnector package (version 7+) to connect to SQL Server. ```R library(DatabaseConnector) connectionDetails <- createConnectionDetails( dbms = "sql server", server = Sys.getenv("CDM5_SQL_SERVER_SERVER"), user = Sys.getenv("CDM5_SQL_SERVER_USER"), password = Sys.getenv("CDM5_SQL_SERVER_PASSWORD"), port = Sys.getenv("CDM5_SQL_SERVER_PORT") ) con <- connect(connectionDetails) cdm <- cdmFromCon(con, cdmSchema = c("cdmv54", "dbo"), writeSchema = c("tempdb", "dbo")) disconnect(con) ``` -------------------------------- ### Connect to Databricks/Spark via ODBC Source: https://darwin-eu.github.io/CDMConnector/articles/a04_DBI_connection_examples.html Establishes a connection to Databricks using the odbc package. ```R con <- DBI::dbConnect( odbc::databricks(), httpPath = Sys.getenv("DATABRICKS_HTTPPATH"), useNativeQuery = FALSE ) cdm <- cdmFromCon(con, cdmSchema = "gibleed", writeSchema = "scratch") DBI::dbDisconnect(con) ``` -------------------------------- ### Connect to Synpuf 5.3 Dataset with Achilles Source: https://darwin-eu.github.io/CDMConnector/reference/eunomiaDir.html Connects to the synpuf-1k Eunomia dataset in CDM version 5.3, including Achilles tables. Requires the dataset to be available. ```R con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("synpuf-1k", "5.3")) cdm <- cdmFromCon(con, "main", "main", achillesSchema = "main") cdmDisconnect(cdm) ``` -------------------------------- ### Require Eunomia Database Source: https://darwin-eu.github.io/CDMConnector/reference/requireEunomia.html Ensures that the Eunomia package is available and can be used to create a database. Specify the dataset name and CDM version if needed. ```r requireEunomia(datasetName = "GiBleed", cdmVersion = "5.3") ``` -------------------------------- ### POST generateCohortSet2 Source: https://darwin-eu.github.io/CDMConnector/reference/generateCohortSet2.html Generates cohort sets using an optimized DAG-based SQL pipeline that does not require Java or CirceR. ```APIDOC ## POST generateCohortSet2 ### Description Generates cohort sets using an optimized DAG-based SQL pipeline that does not require Java or CirceR. This is a faster alternative to generateCohortSet that produces equivalent results. ### Method POST ### Parameters #### Request Body - **cdm** (cdm_reference) - Required - A cdm reference created by CDMConnector. write_schema must be specified. - **cohortSet** (dataframe) - Required - A cohortSet dataframe created with readCohortSet. - **name** (string) - Required - Name of the cohort table to be created. Must be a lowercase character string that starts with a letter and only contains letters, numbers, and underscores. - **computeAttrition** (boolean) - Optional - Should attrition be computed? TRUE (default) or FALSE. - **overwrite** (boolean) - Optional - Should the cohort table be overwritten if it already exists? TRUE (default) or FALSE. - **cache** (boolean) - Optional - Logical; if TRUE, enable incremental DAG caching. Default FALSE. ### Response #### Success Response (200) - **cdm** (cdm_reference) - A cdm reference with the generated cohort table added. ```