darwin-eu / CDMConnector

A pipe friendly way to interact with an OMOP Common Data Model
https://darwin-eu.github.io/CDMConnector/
Apache License 2.0
12 stars 10 forks source link

Declare other table in cdm_from_con() #31

Open JTBrash opened 4 days ago

JTBrash commented 4 days ago

When creating the cdm object, would it be possible to declare 'other tables' from the write_schema (or in the database). Note, I'm referring to non-cohort tables.

I'd imagine it would look something like this:

cdm <- CDMConnector::cdm_from_con(con,
                                  cdm_schema = cdm_schema,
                                  write_schema = write_schema,
                                  cohort_tables = "cohort",
                                  tables = "other_tables")

I'm suggesting this as I'm having difficulty using a non-cohort table in the CDM after I use insertTable(). Essentially I don't see the table in the CDM object after inserting, and as a result thought this might be a useful feature for CDMConnector.

ablack3 commented 1 day ago

Hi @JTBrash,

Will this work?

library(CDMConnector)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
con <- DBI::dbConnect(duckdb::duckdb(), eunomia_dir())
cdm <- cdmFromCon(con, "main", "main")
#> Note: method with signature 'DBIConnection#Id' chosen for function 'dbExistsTable',
#>  target signature 'duckdb_connection#Id'.
#>  "duckdb_connection#ANY" would also be valid

# inserting a dataframe into the cdm object
cdm <- insertTable(cdm, "cars", cars)
cdm$cars
#> # Source:   table<main.cars> [?? x 2]
#> # Database: DuckDB v1.1.2 [root@Darwin 23.1.0:R 4.3.3//private/var/folders/2j/8z0yfn1j69q8sxjc7vj9yhz40000gp/T/RtmpA9ZCX7/filecc4d1e80c4db.duckdb]
#>    speed  dist
#>    <dbl> <dbl>
#>  1     4     2
#>  2     4    10
#>  3     7     4
#>  4     7    22
#>  5     8    16
#>  6     9    10
#>  7    10    18
#>  8    10    26
#>  9    10    34
#> 10    11    17
#> # ℹ more rows

# inserting an existing table into the cdm
DBI::dbWriteTable(con, "cars2", cars)

cdm$cars2 <- 
  dplyr::tbl(con, I("main.cars2")) %>% 
  omopgenerics::newCdmTable(src = omopgenerics::cdmSource(cdm), name = "cars2")

cdm$cars2
#> # Source:   SQL [?? x 2]
#> # Database: DuckDB v1.1.2 [root@Darwin 23.1.0:R 4.3.3//private/var/folders/2j/8z0yfn1j69q8sxjc7vj9yhz40000gp/T/RtmpA9ZCX7/filecc4d1e80c4db.duckdb]
#>    speed  dist
#>    <dbl> <dbl>
#>  1     4     2
#>  2     4    10
#>  3     7     4
#>  4     7    22
#>  5     8    16
#>  6     9    10
#>  7    10    18
#>  8    10    26
#>  9    10    34
#> 10    11    17
#> # ℹ more rows

cdmDisconnect(cdm)

Created on 2024-11-25 with reprex v2.1.1