Title: | Connect to an OMOP Common Data Model |
---|---|
Description: | Provides tools for working with observational health data in the Observational Medical Outcomes Partnership (OMOP) Common Data Model format with a pipe friendly syntax. Common data model database table references are stored in a single compound object along with metadata. |
Authors: | Adam Black [aut, cre] |
Maintainer: | Adam Black <[email protected]> |
License: | Apache License (>= 2) |
Version: | 2.0.0 |
Built: | 2025-02-26 15:24:29 UTC |
Source: | https://github.com/darwin-eu/cdmconnector |
Run a dplyr query and add the result set to an existing
appendPermanent(x, name, schema = NULL)
appendPermanent(x, name, schema = NULL)
x |
A dplyr query |
name |
Name of the table to be appended. If it does not already exist it will be created. |
schema |
Schema where the table exists. Can be a length 1 or 2 vector. (e.g. schema = "my_schema", schema = c("my_schema", "dbo")) |
A dplyr reference to the newly created table
## Not run: 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) ## End(Not run)
## Not run: 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) ## End(Not run)
This is a workaround for using as.Date inside dplyr verbs against a database
backend. This function should only be used inside dplyr verbs where the first
argument is a database table reference. asDate
must be unquoted with !! inside
dplyr verbs (see example).
asDate(x)
asDate(x)
x |
an R expression |
## Not run: con <- DBI::dbConnect(odbc::odbc(), "Oracle") date_tbl <- dplyr::copy_to(con, data.frame(y = 2000L, m = 10L, d = 10L), name = "tmp", temporary = TRUE) df <- date_tbl %>% dplyr::mutate(date_from_parts = !!asDate(paste0( .data$y, "/", .data$m, "/", .data$d ))) %>% dplyr::collect() ## End(Not run)
## Not run: con <- DBI::dbConnect(odbc::odbc(), "Oracle") date_tbl <- dplyr::copy_to(con, data.frame(y = 2000L, m = 10L, d = 10L), name = "tmp", temporary = TRUE) df <- date_tbl %>% dplyr::mutate(date_from_parts = !!asDate(paste0( .data$y, "/", .data$m, "/", .data$d ))) %>% dplyr::collect() ## End(Not run)
Run benchmark of tasks using CDMConnector
benchmarkCDMConnector(cdm)
benchmarkCDMConnector(cdm)
cdm |
A CDM reference object |
a tibble with time taken for different analyses
## Not run: library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") benchmarkCDMConnector(cdm) DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") benchmarkCDMConnector(cdm) DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
Get underlying database connection
cdmCon(cdm)
cdmCon(cdm)
cdm |
A cdm reference object created by |
A reference to the database containing tables in the cdm reference
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con = con, cdmName = "Eunomia", cdmSchema = "main", writeSchema = "main") cdmCon(cdm) DBI::dbDisconnect(con) ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con = con, cdmName = "Eunomia", cdmSchema = "main", writeSchema = "main") cdmCon(cdm) DBI::dbDisconnect(con) ## End(Not run)
This function will disconnect from the database as well as drop "temporary" tables that were created on database systems that do not support actual temporary tables. Currently temp tables are emulated on Spark/Databricks systems.
## S3 method for class 'db_cdm' cdmDisconnect(cdm, dropWriteSchema = FALSE, ...)
## S3 method for class 'db_cdm' cdmDisconnect(cdm, dropWriteSchema = FALSE, ...)
cdm |
cdm reference |
dropWriteSchema |
Whether to drop tables in the writeSchema |
... |
Not used |
This experimental function transforms the OMOP CDM into a single observation table. This is only recommended for use with a filtered CDM or a cdm that is small in size.
cdmFlatten( cdm, domain = c("condition_occurrence", "drug_exposure", "procedure_occurrence"), includeConceptName = TRUE )
cdmFlatten( cdm, domain = c("condition_occurrence", "drug_exposure", "procedure_occurrence"), includeConceptName = TRUE )
cdm |
A cdm_reference object |
domain |
Domains to include. Must be a subset of "condition_occurrence", "drug_exposure", "procedure_occurrence", "measurement", "visit_occurrence", "death", "observation" |
includeConceptName |
Should concept_name and type_concept_name be include in the output table? TRUE (default) or FALSE |
A lazy query that when evaluated will result in a single table
## Not run: library(CDMConnector) library(dplyr, warn.conflicts = FALSE) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main") all_observations <- cdmSubset(cdm, personId = c(2, 18, 42)) %>% cdmFlatten() %>% collect() all_observations #> # A tibble: 213 × 8 #> person_id observation_. start_date end_date type_. domain obser. type_. #> <dbl> <dbl> <date> <date> <dbl> <chr> <chr> <chr> #> 1 2 40213201 1986-09-09 1986-09-09 5.81e5 drug pneumo <NA> #> 2 18 4116491 1997-11-09 1998-01-09 3.20e4 condi Escher <NA> #> 3 18 40213227 2017-01-04 2017-01-04 5.81e5 drug tetanu <NA> #> 4 42 4156265 1974-06-13 1974-06-27 3.20e4 condi Facial <NA> #> 5 18 40213160 1966-02-23 1966-02-23 5.81e5 drug poliov <NA> #> 6 42 4198190 1933-10-29 1933-10-29 3.80e7 proce Append <NA> #> 7 2 4109685 1952-07-13 1952-07-27 3.20e4 condi Lacera <NA> #> 8 18 40213260 2017-01-04 2017-01-04 5.81e5 drug zoster <NA> #> 9 42 4151422 1985-02-03 1985-02-03 3.80e7 proce Sputum <NA> #> 10 2 4163872 1993-03-29 1993-03-29 3.80e7 proce Plain <NA> #> # ... with 203 more rows, and abbreviated variable names observation_concept_id, #> # type_concept_id, observation_concept_name, type_concept_name DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: library(CDMConnector) library(dplyr, warn.conflicts = FALSE) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main") all_observations <- cdmSubset(cdm, personId = c(2, 18, 42)) %>% cdmFlatten() %>% collect() all_observations #> # A tibble: 213 × 8 #> person_id observation_. start_date end_date type_. domain obser. type_. #> <dbl> <dbl> <date> <date> <dbl> <chr> <chr> <chr> #> 1 2 40213201 1986-09-09 1986-09-09 5.81e5 drug pneumo <NA> #> 2 18 4116491 1997-11-09 1998-01-09 3.20e4 condi Escher <NA> #> 3 18 40213227 2017-01-04 2017-01-04 5.81e5 drug tetanu <NA> #> 4 42 4156265 1974-06-13 1974-06-27 3.20e4 condi Facial <NA> #> 5 18 40213160 1966-02-23 1966-02-23 5.81e5 drug poliov <NA> #> 6 42 4198190 1933-10-29 1933-10-29 3.80e7 proce Append <NA> #> 7 2 4109685 1952-07-13 1952-07-27 3.20e4 condi Lacera <NA> #> 8 18 40213260 2017-01-04 2017-01-04 5.81e5 drug zoster <NA> #> 9 42 4151422 1985-02-03 1985-02-03 3.80e7 proce Sputum <NA> #> 10 2 4163872 1993-03-29 1993-03-29 3.80e7 proce Plain <NA> #> # ... with 203 more rows, and abbreviated variable names observation_concept_id, #> # type_concept_id, observation_concept_name, type_concept_name DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
Create a CDM reference object from a database connection
cdmFromCon( con, cdmSchema, writeSchema, cohortTables = NULL, cdmVersion = NULL, cdmName = NULL, achillesSchema = NULL, .softValidation = FALSE, writePrefix = NULL )
cdmFromCon( con, cdmSchema, writeSchema, cohortTables = NULL, cdmVersion = NULL, cdmName = NULL, achillesSchema = NULL, .softValidation = FALSE, writePrefix = NULL )
con |
A DBI database connection to a database where an OMOP CDM v5.4 or v5.3 instance is located. |
cdmSchema |
The schema where the OMOP CDM tables are located. Defaults to NULL. |
writeSchema |
An optional schema in the CDM database that the user has write access to. |
cohortTables |
A character vector listing the cohort table names to be included in the CDM object. |
cdmVersion |
The version of the OMOP CDM. Cam be "5.3", "5.4", or NULL (default). If NULL we will attempt to automatically determine the cdm version using the cdm_source table and heuristics. |
cdmName |
The name of the CDM. If NULL (default) the cdm_source_name . field in the CDM_SOURCE table will be used. |
achillesSchema |
An optional schema in the CDM database that contains achilles tables. |
.softValidation |
Normally the observation period table should not
have overlapping observation periods for a single person. If |
writePrefix |
A prefix that will be added to all tables created in the write_schema. This can be used to create namespace in your database write_schema for your tables. |
cdmFromCon creates a new cdm reference object from a DBI database connection. In addition to the connection the user needs to pass in the schema in the database where the cdm data can be found as well as another schema where the user has write access to create tables. Nearly all downstream analytic packages need the ability to create temporary data in the database so the write_schema is required.
Some database systems have the idea of a catalog or a compound schema with two components. See examples below for how to pass in catalogs and schemas.
You can also specify a writePrefix
. This is a short character string that will be added
to any tables created in the writeSchema
effectively a namespace in the schema just for your
analysis. If the write_schema is a shared between multiple users setting a unique write_prefix
ensures you do not overwrite existing tables and allows you to easily clean up tables by
dropping all tables that start with the prefix.
A list of dplyr database table references pointing to CDM tables
## Not run: 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) ## End(Not run)
## Not run: 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) ## End(Not run)
cdmSample
takes a cdm object and returns a new cdm that includes only a
random sample of persons in the cdm. Only person_id
s in both the person
table and observation_period table will be considered.
cdmSample(cdm, n, seed = sample.int(1e+06, 1), name = "person_sample")
cdmSample(cdm, n, seed = sample.int(1e+06, 1), name = "person_sample")
cdm |
A cdm_reference object. |
n |
Number of persons to include in the cdm. |
seed |
Seed for the random number generator. |
name |
Name of the table that will contain the sample of persons. |
A modified cdm_reference object where all clinical tables are lazy queries pointing to subset
## Not run: library(CDMConnector) library(dplyr, warn.conflicts = FALSE) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main") cdmSampled <- cdmSample(cdm, n = 2) cdmSampled$person %>% select(person_id) #> # Source: SQL [2 x 1] #> # Database: DuckDB 0.6.1 #> person_id #> <dbl> #> 1 155 #> 2 3422 DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: library(CDMConnector) library(dplyr, warn.conflicts = FALSE) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main") cdmSampled <- cdmSample(cdm, n = 2) cdmSampled$person %>% select(person_id) #> # Source: SQL [2 x 1] #> # Database: DuckDB 0.6.1 #> person_id #> <dbl> #> 1 155 #> 2 3422 DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
cdmSubset
takes a cdm object and a list of person IDs as input. It
returns a new cdm that includes data only for persons matching the provided
person IDs. Generated cohorts in the cdm will also be subset to
the IDs provided.
cdmSubset(cdm, personId)
cdmSubset(cdm, personId)
cdm |
A cdm_reference object |
personId |
A numeric vector of person IDs to include in the cdm |
A modified cdm_reference object where all clinical tables are lazy queries pointing to subset
## Not run: library(CDMConnector) library(dplyr, warn.conflicts = FALSE) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main") cdm2 <- cdmSubset(cdm, personId = c(2, 18, 42)) cdm2$person %>% select(1:3) #> # Source: SQL [3 x 3] #> # Database: DuckDB 0.6.1 #> person_id gender_concept_id year_of_birth #> <dbl> <dbl> <dbl> #> 1 2 8532 1920 #> 2 18 8532 1965 #> 3 42 8532 1909 DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: library(CDMConnector) library(dplyr, warn.conflicts = FALSE) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main") cdm2 <- cdmSubset(cdm, personId = c(2, 18, 42)) cdm2$person %>% select(1:3) #> # Source: SQL [3 x 3] #> # Database: DuckDB 0.6.1 #> person_id gender_concept_id year_of_birth #> <dbl> <dbl> <dbl> #> 1 2 8532 1920 #> 2 18 8532 1965 #> 3 42 8532 1909 DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
cdmSubset
will return a new cdm object that contains lazy queries pointing
to each of the cdm tables but subset to individuals in a generated cohort.
Since the cdm tables are lazy queries, the subset operation will only be
done when the tables are used. computeQuery
can be used to run the SQL
used to subset a cdm table and store it as a new table in the database.
cdmSubsetCohort(cdm, cohortTable = "cohort", cohortId = NULL, verbose = FALSE)
cdmSubsetCohort(cdm, cohortTable = "cohort", cohortId = NULL, verbose = FALSE)
cdm |
A cdm_reference object |
cohortTable |
The name of a cohort table in the cdm reference |
cohortId |
IDs of the cohorts that we want to subset from the cohort table. If NULL (default) all cohorts in cohort table are considered. |
verbose |
Should subset messages be printed? TRUE or FALSE (default) |
A modified cdm_reference with all clinical tables subset to just the persons in the selected cohorts.
## Not run: 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 #> <dbl> #> 1 237 cdm$person %>% tally() #> # Source: SQL [1 x 1] #> # Database: DuckDB 0.6.1 #> n #> <dbl> #> 1 2694 DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: 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 #> <dbl> #> 1 237 cdm$person %>% tally() #> # Source: SQL [1 x 1] #> # Database: DuckDB 0.6.1 #> n #> <dbl> #> 1 2694 DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
Get cdm write schema
cdmWriteSchema(cdm)
cdmWriteSchema(cdm)
cdm |
A cdm reference object created by |
The database write schema
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con = con, cdmName = "Eunomia", cdmSchema = "main", writeSchema = "main") cdmWriteSchema(cdm) DBI::dbDisconnect(con) ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con = con, cdmName = "Eunomia", cdmSchema = "main", writeSchema = "main") cdmWriteSchema(cdm) DBI::dbDisconnect(con) ## End(Not run)
This function is a wrapper around dplyr::compute
that is tested on several
database systems. It is needed to handle edge cases where dplyr::compute
does not produce correct SQL.
computeQuery( x, name = uniqueTableName(), temporary = TRUE, schema = NULL, overwrite = TRUE, ... )
computeQuery( x, name = uniqueTableName(), temporary = TRUE, schema = NULL, overwrite = TRUE, ... )
x |
A dplyr query |
name |
The name of the table to create. |
temporary |
Should the table be temporary: TRUE (default) or FALSE |
schema |
The schema where the table should be created. Ignored if temporary = TRUE. |
overwrite |
Should the table be overwritten if it already exists: TRUE (default) or FALSE Ignored if temporary = TRUE. |
... |
Further arguments passed on the |
A dplyr::tbl()
reference to the newly created table.
## Not run: 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) ## End(Not run)
## Not run: 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) ## End(Not run)
It may be helpful to be able to easily copy a small test cdm from a local database to a remote for testing. copyCdmTo takes a cdm object and a connection. It copies the cdm to the remote database connection. CDM tables can be prefixed in the new database allowing for multiple cdms in a single shared database schema.
copyCdmTo(con, cdm, schema, overwrite = FALSE)
copyCdmTo(con, cdm, schema, overwrite = FALSE)
con |
A DBI datbase connection created by |
cdm |
A cdm reference object created by |
schema |
schema name in the remote database where the user has write permission |
overwrite |
Should the cohort table be overwritten if it already exists? TRUE or FALSE (default) |
A cdm reference object pointing to the newly created cdm in the remote database
This function must be "unquoted" using the "bang bang" operator (!!). See example.
dateadd(date, number, interval = "day")
dateadd(date, number, interval = "day")
date |
The name of a date column in the database table as a character string |
number |
The number of units to add. Can be a positive or negative whole number. |
interval |
The units to add. Must be either "day" (default) or "year" |
Platform specific SQL that can be used in a dplyr query.
## Not run: con <- DBI::dbConnect(duckdb::duckdb()) date_tbl <- dplyr::copy_to(con, data.frame(date1 = as.Date("1999-01-01")), name = "tmpdate", overwrite = TRUE, temporary = TRUE) df <- date_tbl %>% dplyr::mutate(date2 = !!dateadd("date1", 1, interval = "year")) %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb()) date_tbl <- dplyr::copy_to(con, data.frame(date1 = as.Date("1999-01-01")), name = "tmpdate", overwrite = TRUE, temporary = TRUE) df <- date_tbl %>% dplyr::mutate(date2 = !!dateadd("date1", 1, interval = "year")) %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
This function must be "unquoted" using the "bang bang" operator (!!). See example.
datediff(start, end, interval = "day")
datediff(start, end, interval = "day")
start |
The name of the start date column in the database as a string. |
end |
The name of the end date column in the database as a string. |
interval |
The units to use for difference calculation. Must be either "day" (default) or "year". |
Platform specific SQL that can be used in a dplyr query.
## Not run: con <- DBI::dbConnect(duckdb::duckdb()) date_tbl <- dplyr::copy_to(con, data.frame(date1 = as.Date("1999-01-01")), name = "tmpdate", overwrite = TRUE, temporary = TRUE) df <- date_tbl %>% dplyr::mutate(date2 = !!dateadd("date1", 1, interval = "year")) %>% dplyr::mutate(dif_years = !!datediff("date1", "date2", interval = "year")) %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb()) date_tbl <- dplyr::copy_to(con, data.frame(date1 = as.Date("1999-01-01")), name = "tmpdate", overwrite = TRUE, temporary = TRUE) df <- date_tbl %>% dplyr::mutate(date2 = !!dateadd("date1", 1, interval = "year")) %>% dplyr::mutate(dif_years = !!datediff("date1", "date2", interval = "year")) %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
Extract the day, month or year of a date in a dplyr pipeline
datepart(date, interval = "year", dbms = NULL)
datepart(date, interval = "year", dbms = NULL)
date |
Character string that represents to a date column. |
interval |
Interval to extract from a date. Valid options are "year", "month", or "day". |
dbms |
Database system, if NULL it is auto detected. |
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), ":memory:") date_tbl <- dplyr::copy_to(con, data.frame(birth_date = as.Date("1993-04-19")), name = "tmp", temporary = TRUE) df <- date_tbl %>% dplyr::mutate(year = !!datepart("birth_date", "year")) %>% dplyr::mutate(month = !!datepart("birth_date", "month")) %>% dplyr::mutate(day = !!datepart("birth_date", "day")) %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), ":memory:") date_tbl <- dplyr::copy_to(con, data.frame(birth_date = as.Date("1993-04-19")), name = "tmp", temporary = TRUE) df <- date_tbl %>% dplyr::mutate(year = !!datepart("birth_date", "year")) %>% dplyr::mutate(month = !!datepart("birth_date", "month")) %>% dplyr::mutate(day = !!datepart("birth_date", "day")) %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
Get the database management system (dbms) from a cdm_reference or DBI connection
dbms(con)
dbms(con)
con |
A DBI connection or cdm_reference |
A character string representing the dbms that can be used with SqlRender
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con) dbms(cdm) dbms(con) ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) cdm <- cdmFromCon(con) dbms(cdm) dbms(con) ## End(Not run)
Create a source for a cdm in a database.
dbSource(con, writeSchema)
dbSource(con, writeSchema)
con |
Connection to a database. |
writeSchema |
Schema where cohort tables are. You must have read and write access to it. |
Download the Eunomia data files from https://github.com/darwin-eu/EunomiaDatasets
downloadEunomiaData( datasetName = "GiBleed", cdmVersion = "5.3", pathToData = Sys.getenv("EUNOMIA_DATA_FOLDER"), overwrite = FALSE )
downloadEunomiaData( datasetName = "GiBleed", cdmVersion = "5.3", pathToData = Sys.getenv("EUNOMIA_DATA_FOLDER"), overwrite = FALSE )
datasetName |
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 |
The OMOP CDM version. This version will appear in the suffix of the data file, for example: synpuf_5.3.zip. Must be '5.3' (default) or '5.4'. |
pathToData |
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 |
Control whether the existing archive file will be overwritten should it already exist. |
Invisibly returns the destination if the download was successful.
## Not run: downloadEunomiaData("GiBleed") ## End(Not run)
## Not run: downloadEunomiaData("GiBleed") ## End(Not run)
Tables will be dropped from the write schema of the cdm.
dropTable.db_cdm(cdm, name)
dropTable.db_cdm(cdm, name)
cdm |
a cdm_reference object |
name |
A character vector of table names to be dropped |
Eunomia is an OHDSI project that provides several example OMOP CDM datasets for testing and development. This function creates a copy of a Eunomia database in duckdb and returns the path to the new database file. If the dataset does not yet exist on the user's computer it will attempt to download the source data to the the path defined by the EUNOMIA_DATA_FOLDER environment variable.
eunomiaDir( datasetName = "GiBleed", cdmVersion = "5.3", databaseFile = tempfile(fileext = ".duckdb") )
eunomiaDir( datasetName = "GiBleed", cdmVersion = "5.3", databaseFile = tempfile(fileext = ".duckdb") )
datasetName |
One of "GiBleed" (default), "synthea-allergies-10k", "synthea-anemia-10k", "synthea-breast_cancer-10k", "synthea-contraceptives-10k", "synthea-covid19-10k", "synthea-covid19-200k", "synthea-dermatitis-10k", "synthea-heart-10k", "synthea-hiv-10k", "synthea-lung_cancer-10k", "synthea-medications-10k", "synthea-metabolic_syndrome-10k", "synthea-opioid_addiction-10k", "synthea-rheumatoid_arthritis-10k", "synthea-snf-10k", "synthea-surgery-10k", "synthea-total_joint_replacement-10k", "synthea-veteran_prostate_cancer-10k", "synthea-veterans-10k", "synthea-weight_loss-10k", "empty_cdm", "synpuf-1k" |
cdmVersion |
The OMOP CDM version. Must be "5.3" or "5.4". |
databaseFile |
The full path to the new copy of the example CDM dataset. |
Most of the Eunomia datasets available in CDMConnector are from the Synthea project. Synthea is an open-source synthetic patient generator that models the medical history of synthetic patients. The Synthea datasets are generated using the Synthea tool and then converted to the OMOP CDM format using the OHDSI ETL-Synthea project https://ohdsi.github.io/ETL-Synthea/. Currently the synthea datasets are only available in the OMOP CDM v5.3 format. See https://synthetichealth.github.io/synthea/ for details on the Synthea project.
In addition to Synthea, the Eunomia project provides the CMS Synthetic Public Use Files (SynPUFs) in both 5.3 and 5.4 OMOP CDM formats. This data is synthetic US Medicare claims data mapped to OMOP CDM format. The OMOP CDM has a set of optional metadata tables, called Achilles tables, that include pre-computed analytics about the entire dataset such as record and person counts. The Eunomia Synpuf datasets include the Achilles tables.
Eunomia also provides empty cdms that can be used as a starting point for creating a new example CDM. This is useful for creating test data for studies or analytic packages. The empty CDM includes the vocabulary tables and all OMOP CDM tables but the clinical tables are empty and need to be populated with data. For additional information on creating small test CDM datasets see https://ohdsi.github.io/omock/ and https://darwin-eu.github.io/TestGenerator/.
To contribute synthetic observational health data to the Eunomia project please open an issue at https://github.com/OHDSI/Eunomia/issues/
Setup: To use the eunomiaDir
function please set the EUNOMIA_DATA_FOLDER
in your .Renviron file
to a folder on your computer where the datasets will be downloaded to. This file can
be opened by calling usethis::edit_r_environ()
.
The file path to the new Eunomia dataset copy
## Not run: # The defaults GiBleed dataset is a small dataset that is useful for testing library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, "main", "main") cdmDisconnect(cdm) # Synpuf datasets include the Achilles tables con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("synpuf-1k", "5.3")) cdm <- cdmFromCon(con, "main", "main", achillesSchema = "main") cdmDisconnect(cdm) # Currently the only 5.4 dataset is synpuf-1k con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("synpuf-1k", "5.4")) cdm <- cdmFromCon(con, "main", "main", achillesSchema = "main") cdmDisconnect(cdm) ## End(Not run)
## Not run: # The defaults GiBleed dataset is a small dataset that is useful for testing library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, "main", "main") cdmDisconnect(cdm) # Synpuf datasets include the Achilles tables con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("synpuf-1k", "5.3")) cdm <- cdmFromCon(con, "main", "main", achillesSchema = "main") cdmDisconnect(cdm) # Currently the only 5.4 dataset is synpuf-1k con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("synpuf-1k", "5.4")) cdm <- cdmFromCon(con, "main", "main", achillesSchema = "main") cdmDisconnect(cdm) ## End(Not run)
Has the Eunomia dataset been cached?
eunomiaIsAvailable(datasetName = "GiBleed", cdmVersion = "5.3")
eunomiaIsAvailable(datasetName = "GiBleed", cdmVersion = "5.3")
datasetName |
Name of the Eunomia dataset to check. Defaults to "GiBleed". |
cdmVersion |
Version of the Eunomia dataset to check. Must be "5.3" or "5.4". |
TRUE if the eunomia example dataset is available and FASLE otherwise
List the available example CDM datasets
exampleDatasets()
exampleDatasets()
A character vector with example CDM dataset identifiers
## Not run: library(CDMConnector) exampleDatasets()[1] #> [1] "GiBleed" con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("GiBleed")) cdm <- cdmFromCon(con) ## End(Not run)
## Not run: library(CDMConnector) exampleDatasets()[1] #> [1] "GiBleed" con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir("GiBleed")) cdm <- cdmFromCon(con) ## End(Not run)
A "chort_table" object consists of four components
A remote table reference to an OHDSI cohort table with at least the columns: cohort_definition_id, subject_id, cohort_start_date, cohort_end_date. Additional columns are optional and some analytic packages define additional columns specific to certain analytic cohorts.
A settings attribute which points to a remote table containing cohort settings including the names of the cohorts.
An attrition attribute which points to a remote table with attrition information recorded during generation. This attribute is optional. Since calculating attrition takes additional compute it can be skipped resulting in a NULL attrition attribute.
A cohortCounts attribute which points to a remote table containing cohort counts
Each of the three attributes are tidy tables. The implementation of this object is experimental and user feedback is welcome.
One key design principle is that cohort_table objects are created once
and can persist across analysis execution but should not be modified after
creation. While it is possible to modify a cohort_table object doing
so will invalidate it and it's attributes may no longer be accurate.
generateCohortSet( cdm, cohortSet, name, computeAttrition = TRUE, overwrite = TRUE )
generateCohortSet( cdm, cohortSet, name, computeAttrition = TRUE, overwrite = TRUE )
cdm |
A cdm reference created by CDMConnector. write_schema must be specified. |
cohortSet |
A cohortSet dataframe created with |
name |
Name of the cohort table to be created. This will also be used as a prefix for the cohort attribute tables. This must be a lowercase character string that starts with a letter and only contains letters, numbers, and underscores. |
computeAttrition |
Should attrition be computed? TRUE (default) or FALSE |
overwrite |
Should the cohort table be overwritten if it already exists? TRUE (default) or FALSE |
## Not run: 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) ## End(Not run)
## Not run: 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) ## End(Not run)
Generate a new cohort set from one or more concept sets. Each concept set will result in one cohort and represent the time during which the concept was observed for each subject/person. Concept sets can be passed to this function as:
A named list of numeric vectors, one vector per concept set
A named list of Capr concept sets
Clinical observation records will be looked up in the respective domain tables using the vocabulary in the CDM. If a required domain table does not exist in the cdm object a warning will be given. Concepts that are not in the vocabulary or in the data will be silently ignored. If end dates are missing or do not exist, as in the case of the procedure and observation domains, the the start date will be used as the end date.
generateConceptCohortSet( cdm, conceptSet = NULL, name, limit = "first", requiredObservation = c(0, 0), end = "observation_period_end_date", subsetCohort = NULL, subsetCohortId = NULL, overwrite = TRUE )
generateConceptCohortSet( cdm, conceptSet = NULL, name, limit = "first", requiredObservation = c(0, 0), end = "observation_period_end_date", subsetCohort = NULL, subsetCohortId = NULL, overwrite = TRUE )
cdm |
A cdm reference object created by |
conceptSet |
A named list of numeric vectors or a Concept Set Expression created
|
name |
The name of the new generated cohort table as a character string |
limit |
Include "first" (default) or "all" occurrences of events in the cohort
|
requiredObservation |
A numeric vector of length 2 that specifies the number of days of required observation time prior to index and post index for an event to be included in the cohort. |
end |
How should the
|
subsetCohort |
A cohort table containing the individuals for which to generate cohorts for. Only individuals in the cohort table will appear in the created generated cohort set. |
subsetCohortId |
A set of cohort IDs from the cohort table for which to include. If none are provided, all cohorts in the cohort table will be included. |
overwrite |
Should the cohort table be overwritten if it already exists? TRUE (default) or FALSE. |
A cdm reference object with the new generated cohort set table added
This is similar to dbplyr::in_schema but has been tested across multiple database platforms. It only exists to work around some of the limitations of dbplyr::in_schema.
inSchema(schema, table, dbms = NULL)
inSchema(schema, table, dbms = NULL)
schema |
A schema name as a character string |
table |
A table name as character string |
dbms |
The name of the database management system as returned
by |
A DBI::Id that represents a qualified table and schema
DBI::dbListTables can be used to get all tables in a database but not always in a
specific schema. listTables
will list tables in a schema.
listTables(con, schema = NULL)
listTables(con, schema = NULL)
con |
A DBI connection to a database |
schema |
The name of a schema in a database. If NULL, returns DBI::dbListTables(con). |
A character vector of table names
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) listTables(con, schema = "main") ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomiaDir()) listTables(con, schema = "main") ## End(Not run)
A "cohort set" is a collection of cohort definitions. In R this is stored in a dataframe with cohort_definition_id, cohort_name, and cohort columns. On disk this is stored as a folder with a CohortsToCreate.csv file and one or more json files. If the CohortsToCreate.csv file is missing then all of the json files in the folder will be used, cohort_definition_id will be automatically assigned in alphabetical order, and cohort_name will match the file names.
readCohortSet(path)
readCohortSet(path)
path |
The path to a folder containing Circe cohort definition json files and optionally a csv file named CohortsToCreate.csv with columns cohortId, cohortName, and jsonPath. |
eunomiaDir()
.Require eunomia to be available. The function makes sure that you can later
create a eunomia database with eunomiaDir()
.
requireEunomia(datasetName = "GiBleed", cdmVersion = "5.3")
requireEunomia(datasetName = "GiBleed", cdmVersion = "5.3")
datasetName |
Name of the Eunomia dataset to check. Defaults to "GiBleed". |
cdmVersion |
Version of the Eunomia dataset to check. Must be "5.3" or "5.4". |
Path to eunomia database.
Extract the name, version, and selected record counts from a cdm.
snapshot(cdm)
snapshot(cdm)
cdm |
A cdm object |
A named list of attributes about the cdm including selected fields from the cdm_source table and record counts from the person and observation_period tables
## Not run: library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, "main") snapshot(cdm) DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, "main") snapshot(cdm) DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
This function provides DBMS independent syntax for quantiles estimation.
Can be used by itself or in combination with mutate()
when calculating other aggregate metrics (min, max, mean).
summarise_quantile()
, summarize_quantile()
, summariseQuantile()
and summarizeQuantile()
are synonyms.
summariseQuantile(.data, x = NULL, probs, nameSuffix = "value")
summariseQuantile(.data, x = NULL, probs, nameSuffix = "value")
.data |
lazy data frame backed by a database query. |
x |
column name whose sample quantiles are wanted. |
probs |
numeric vector of probabilities with values in [0,1]. |
nameSuffix |
character; is appended to numerical quantile value as a column name part. |
Implemented quantiles estimation algorithm returns values analogous to
quantile{stats}
with argument type = 1
.
See discussion in Hyndman and Fan (1996).
Results differ from PERCENTILE_CONT
natively implemented in various DBMS,
where returned values are equal to quantile{stats}
with default argument type = 7
An object of the same type as '.data'
## Not run: con <- DBI::dbConnect(duckdb::duckdb()) mtcars_tbl <- dplyr::copy_to(con, mtcars, name = "tmp", overwrite = TRUE, temporary = TRUE) df <- mtcars_tbl %>% dplyr::group_by(cyl) %>% dplyr::mutate(mean = mean(mpg, na.rm = TRUE)) %>% summariseQuantile(mpg, probs = c(0, 0.2, 0.4, 0.6, 0.8, 1), nameSuffix = "quant") %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: con <- DBI::dbConnect(duckdb::duckdb()) mtcars_tbl <- dplyr::copy_to(con, mtcars, name = "tmp", overwrite = TRUE, temporary = TRUE) df <- mtcars_tbl %>% dplyr::group_by(cyl) %>% dplyr::mutate(mean = mean(mpg, na.rm = TRUE)) %>% summariseQuantile(mpg, probs = c(0, 0.2, 0.4, 0.6, 0.8, 1), nameSuffix = "quant") %>% dplyr::collect() DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
The OMOP CDM tables are grouped together and the tblGroup
function allows
users to easily create a CDM reference including one or more table groups.
tblGroup(group)
tblGroup(group)
group |
A character vector of CDM table groups: "vocab", "clinical", "all", "default", "derived". |
The "default" table group is meant to capture the most commonly used set of CDM tables. Currently the "default" group is: person, observation_period, visit_occurrence, visit_detail, condition_occurrence, drug_exposure, procedure_occurrence, device_exposure, measurement, observation, death, note, note_nlp, specimen, fact_relationship, location, care_site, provider, payer_plan_period, cost, drug_era, dose_era, condition_era, concept, vocabulary, concept_relationship, concept_ancestor, concept_synonym, drug_strength
A character vector of CDM tables names in the groups
## Not run: 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")) ## End(Not run)
## Not run: 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")) ## End(Not run)
Extract the CDM version attribute from a cdm_reference object
version(cdm)
version(cdm)
cdm |
A cdm object |
"5.3" or "5.4"
## Not run: library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") version(cdm) DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)
## Not run: library(CDMConnector) con <- DBI::dbConnect(duckdb::duckdb(), eunomiaDir()) cdm <- cdmFromCon(con, cdmSchema = "main", writeSchema = "main") version(cdm) DBI::dbDisconnect(con, shutdown = TRUE) ## End(Not run)