diff --git a/R/unionCohorts.R b/R/unionCohorts.R index d32a0ace..33c6bd9f 100644 --- a/R/unionCohorts.R +++ b/R/unionCohorts.R @@ -68,15 +68,17 @@ unionCohorts <- function(cohort, tmpTable <- omopgenerics::uniqueTableName() unionedCohort <- cohort |> dplyr::filter(.data$cohort_definition_id %in% .env$cohortId) |> - joinOverlap(name = name, by = "subject_id", gap = gap) |> + joinOverlap(name = tmpTable, + by = "subject_id", + gap = gap) |> dplyr::mutate(cohort_definition_id = 1L) |> + dplyr::relocate(dplyr::all_of(omopgenerics::cohortColumns("cohort"))) |> dplyr::compute(name = tmpTable, temporary = FALSE) cohCodelist <- attr(cohort, "cohort_codelist") if (!is.null(cohCodelist)) { cohCodelist <- cohCodelist |> dplyr::mutate("cohort_definition_id" = 1L) } unionedCohort <- unionedCohort |> - dplyr::relocate(dplyr::all_of(omopgenerics::cohortColumns("cohort"))) |> omopgenerics::newCohortTable( cohortSetRef = cohSet, cohortAttritionRef = NULL, @@ -91,6 +93,7 @@ unionCohorts <- function(cohort, cdm <- bind(cohort, unionedCohort, name = name) } + CDMConnector::dropTable(cdm, name = tmpTable) return(cdm[[name]]) } diff --git a/README.Rmd b/README.Rmd index 0d7a6b35..be24754c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,6 +18,7 @@ knitr::opts_chunk$set( [![CRAN status](https://www.r-pkg.org/badges/version/CohortConstructor)](https://CRAN.R-project.org/package=CohortConstructor) [![R-CMD-check](https://github.com/OHDSI/CohortConstructor/workflows/R-CMD-check/badge.svg)](https://github.com/OHDSI/CohortConstructor/actions) +[![Codecov test coverage](https://codecov.io/gh/OHDSI/CohortConstructor/branch/main/graph/badge.svg)](https://app.codecov.io/gh/OHDSI/CohortConstructor?branch=main) [![Lifecycle:Experimental](https://img.shields.io/badge/Lifecycle-Experimental-339999)](https://lifecycle.r-lib.org/articles/stages.html#experimental) diff --git a/README.md b/README.md index 99779494..e7d4e7ca 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ [![CRAN status](https://www.r-pkg.org/badges/version/CohortConstructor)](https://CRAN.R-project.org/package=CohortConstructor) [![R-CMD-check](https://github.com/OHDSI/CohortConstructor/workflows/R-CMD-check/badge.svg)](https://github.com/OHDSI/CohortConstructor/actions) +[![Codecov test +coverage](https://codecov.io/gh/OHDSI/CohortConstructor/branch/main/graph/badge.svg)](https://app.codecov.io/gh/OHDSI/CohortConstructor?branch=main) [![Lifecycle:Experimental](https://img.shields.io/badge/Lifecycle-Experimental-339999)](https://lifecycle.r-lib.org/articles/stages.html#experimental) @@ -112,14 +114,14 @@ cohort_count(cdm$fractures) %>% glimpse() #> Rows: 3 #> Columns: 3 #> $ cohort_definition_id 1, 2, 3 -#> $ number_records 462, 565, 137 -#> $ number_subjects 426, 508, 132 +#> $ number_records 464, 569, 138 +#> $ number_subjects 427, 510, 132 attrition(cdm$fractures) %>% glimpse() #> Rows: 9 #> Columns: 7 #> $ cohort_definition_id 1, 1, 1, 2, 2, 2, 3, 3, 3 -#> $ number_records 462, 462, 462, 565, 565, 565, 137, 137, 137 -#> $ number_subjects 426, 426, 426, 508, 508, 508, 132, 132, 132 +#> $ number_records 464, 464, 464, 569, 569, 569, 138, 138, 138 +#> $ number_subjects 427, 427, 427, 510, 510, 510, 132, 132, 132 #> $ reason_id 1, 2, 3, 1, 2, 3, 1, 2, 3 #> $ reason "Initial qualifying events", "cohort requirements… #> $ excluded_records 0, 0, 0, 0, 0, 0, 0, 0, 0 @@ -134,21 +136,28 @@ our three cohorts to create this overall cohort like so: ``` r cdm$fractures <- unionCohorts(cdm$fractures, - cohortName = "any_fracture", - name ="fractures") + cohortName = "any_fracture", + keepOriginalCohorts = TRUE, + name ="fractures") ``` ``` r settings(cdm$fractures) -#> # A tibble: 1 × 3 -#> cohort_definition_id cohort_name gap -#> -#> 1 1 any_fracture 0 +#> # A tibble: 4 × 5 +#> cohort_definition_id cohort_name cdm_version vocabulary_version gap +#> +#> 1 1 ankle_fracture 5.3 v5.0 18-JAN-19 NA +#> 2 2 forearm_fracture 5.3 v5.0 18-JAN-19 NA +#> 3 3 hip_fracture 5.3 v5.0 18-JAN-19 NA +#> 4 4 any_fracture 0 cohortCount(cdm$fractures) -#> # A tibble: 1 × 3 +#> # A tibble: 4 × 3 #> cohort_definition_id number_records number_subjects #> -#> 1 1 1164 922 +#> 1 1 464 427 +#> 2 2 569 510 +#> 3 3 138 132 +#> 4 4 1171 924 ``` ### Require in date range @@ -168,11 +177,11 @@ attributes have been updated ``` r cohort_count(cdm$fractures) %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 3 -#> $ cohort_definition_id 1 -#> $ number_records 315 -#> $ number_subjects 282 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 108, 152, 62, 322 +#> $ number_subjects 104, 143, 60, 287 attrition(cdm$fractures) %>% filter(reason == "cohort_start_date between 2000-01-01 & 2020-01-01") %>% glimpse() @@ -205,28 +214,28 @@ criteria. attrition(cdm$fractures) %>% filter(reason == "Age requirement: 40 to 65") %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 7 -#> $ cohort_definition_id 1 -#> $ number_records 124 -#> $ number_subjects 118 -#> $ reason_id 4 -#> $ reason "Age requirement: 40 to 65" -#> $ excluded_records 191 -#> $ excluded_subjects 164 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 43, 64, 22, 129 +#> $ number_subjects 43, 62, 22, 122 +#> $ reason_id 6, 6, 6, 4 +#> $ reason "Age requirement: 40 to 65", "Age requirement: 40… +#> $ excluded_records 65, 88, 40, 193 +#> $ excluded_subjects 61, 81, 38, 165 attrition(cdm$fractures) %>% filter(reason == "Sex requirement: Female") %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 7 -#> $ cohort_definition_id 1 -#> $ number_records 64 -#> $ number_subjects 62 -#> $ reason_id 5 -#> $ reason "Sex requirement: Female" -#> $ excluded_records 60 -#> $ excluded_subjects 56 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 19, 37, 12, 68 +#> $ number_subjects 19, 36, 12, 65 +#> $ reason_id 7, 7, 7, 5 +#> $ reason "Sex requirement: Female", "Sex requirement: Fema… +#> $ excluded_records 24, 27, 10, 61 +#> $ excluded_subjects 24, 26, 10, 57 ``` ### Require presence in another cohort @@ -251,15 +260,15 @@ cdm$fractures <- cdm$fractures %>% attrition(cdm$fractures) %>% filter(reason == "Not in cohort gibleed between -Inf & 0 days relative to cohort_start_date") %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 7 -#> $ cohort_definition_id 1 -#> $ number_records 64 -#> $ number_subjects 62 -#> $ reason_id 8 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 14, 30, 10, 54 +#> $ number_subjects 14, 30, 10, 52 +#> $ reason_id 10, 10, 10, 8 #> $ reason "Not in cohort gibleed between -Inf & 0 days rela… -#> $ excluded_records 0 -#> $ excluded_subjects 0 +#> $ excluded_records 5, 7, 2, 14 +#> $ excluded_subjects 5, 6, 2, 13 ``` ``` r