Skip to content

Commit

Permalink
Update clinicalRecords name
Browse files Browse the repository at this point in the history
  • Loading branch information
martaalcalde committed Jul 23, 2024
1 parent b345049 commit 07f2baf
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 259 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

export(plotObservationPeriod)
export(plotRecordCount)
export(summariseClinicalRecords)
export(summariseEntryCharacteristics)
export(summariseObservationPeriod)
export(summariseOmopTable)
export(summarisePersonDays)
export(summariseRecordCount)
export(suppress)
export(tableOmopTable)
export(tableClinicalRecords)
importFrom(magrittr,"%>%")
importFrom(omopgenerics,suppress)
importFrom(rlang,":=")
Expand Down
2 changes: 1 addition & 1 deletion R/summariseClinicalRecords.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' @export
#'
summariseClinicalTables <- function(omopTable,
summariseClinicalRecords <- function(omopTable,
recordsPerPerson = c("mean", "sd", "median", "q25", "q75", "min", "max"),
inObservation = TRUE,
standardConcept = TRUE,
Expand Down
215 changes: 112 additions & 103 deletions R/summariseRecordCount.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Create a gt table from a summarised omop_table.
#'
#' @param omopTable A summarised_result object with the output from summariseOmopTable().
#' @param omopTable An omop table from a cdm object.
#' @param unit Whether to stratify by "year" or by "month"
#' @param unitInterval Number of years or months to stratify with
#' @param ageGroup A list of age groups to stratify results by.
Expand All @@ -11,108 +11,117 @@
#' @export
#'
summariseRecordCount <- function(omopTable, unit = "year", unitInterval = 1, ageGroup = NULL) {

# Initial checks ----
checkOmopTable(omopTable)

if(missing(unit)){unit <- "year"}
if(missing(unitInterval)){unitInterval <- 1}

checkUnit(unit)
checkUnitInterval(unitInterval)
checkAgeGroup(ageGroup)

cdm <- omopgenerics::cdmReference(omopTable)
omopTable <- omopTable |> dplyr::ungroup()

name <- omopgenerics::tableName(omopTable)
result <- omopgenerics::emptySummarisedResult()
date <- startDate(name)

if(omopTable |> dplyr::tally() |> dplyr::pull("n") == 0){
cli::cli_warn(paste0(omopgenerics::tableName(omopTable), " omop table is empty. Returning an empty summarised result."))
return(result)
}

# Create strata variable ----
strata <- dplyr::if_else(is.null(ageGroup), NA, "age_group")
if(is.na(strata)){strata <- NULL}

# Incidence counts ----
omopTable <- omopTable |>
dplyr::select(dplyr::all_of(date), "person_id") |>
PatientProfiles::addAgeQuery(indexDate = date, ageGroup = ageGroup) |>
dplyr::select(-c("age"))

if (name != "observation_period") {
omopTable <- omopTable |>
filterInObservation(indexDate = date)
}

# interval sequence ----
interval <- getIntervalTibble(omopTable, date, date, unit, unitInterval)

# Insert interval table to the cdm ----
cdm <- cdm |>
omopgenerics::insertTable(name = "interval", table = interval)

# Create summarised result
result <- cdm$interval |>
dplyr::cross_join(
omopTable |>
dplyr::rename("incidence_date" = dplyr::all_of(date))
) |>
dplyr::filter(.data$incidence_date >= .data$interval_start_date &
.data$incidence_date <= .data$interval_end_date) |>
dplyr::group_by(.data$interval_group, dplyr::across(dplyr::all_of(strata))) |>
dplyr::summarise("estimate_value" = dplyr::n(), .groups = "drop") |>
dplyr::collect() |>
dplyr::ungroup()

if(!is.null(strata)){
result <- result |>
rbind(
result |>
dplyr::group_by(.data$interval_group) |>
dplyr::summarise(estimate_value = sum(.data$estimate_value), .groups = "drop") |>
dplyr::mutate(age_group = "overall")
) |>
dplyr::rename() |>
dplyr::mutate()
}else{
result <- result |>
dplyr::mutate("age_group" = "overall")
}

result <- result |>
dplyr::mutate(
"estimate_value" = as.character(.data$estimate_value),
"variable_name" = "incidence_records",
) |>
dplyr::rename("variable_level" = "interval_group") |>
visOmopResults::uniteStrata(cols = "age_group") |>
dplyr::mutate(
"result_id" = as.integer(1),
"cdm_name" = omopgenerics::cdmName(omopgenerics::cdmReference(omopTable)),
"group_name" = "omop_table",
"group_level" = name,
"estimate_name" = "count",
"estimate_type" = "integer",
"additional_name" = "time_interval",
"additional_level" = gsub(" to.*","",.data$variable_level)
) |>
omopgenerics::newSummarisedResult(settings = dplyr::tibble(
"result_id" = 1L,
"result_type" = "summarised_table_counts",
"package_name" = "OmopSketch",
"package_version" = as.character(utils::packageVersion("OmopSketch")),
"unit" = .env$unit,
"unitInterval" = .env$unitInterval
))

omopgenerics::dropTable(cdm = cdm, name = "interval")

return(result)
#
# # Initial checks ----
# checkOmopTable(omopTable)
#
# if(missing(unit)){unit <- "year"}
# if(missing(unitInterval)){unitInterval <- 1}
#
# checkUnit(unit)
# checkUnitInterval(unitInterval)
# checkAgeGroup(ageGroup)
#
# cdm <- omopgenerics::cdmReference(omopTable)
# omopTable <- omopTable |> dplyr::ungroup()
#
# name <- omopgenerics::tableName(omopTable)
# result <- omopgenerics::emptySummarisedResult()
# date <- startDate(name)
#
# if(omopTable |> dplyr::tally() |> dplyr::pull("n") == 0){
# cli::cli_warn(paste0(omopgenerics::tableName(omopTable), " omop table is empty. Returning an empty summarised result."))
# return(result)
# }
#
# # Create strata variable ----
# strata <- dplyr::if_else(is.null(ageGroup), NA, "age_group")
# if(is.na(strata)){strata <- NULL}
#
# # Incidence counts ----
# omopTable <- omopTable |>
# dplyr::select(dplyr::all_of(date), "person_id") |>
# PatientProfiles::addAgeQuery(indexDate = date, ageGroup = ageGroup) |>
# dplyr::select(-tidyselect::any_of(c("age")))
#
# if (name != "observation_period") {
# omopTable <- omopTable |>
# filterInObservation(indexDate = date)
# }
#
# # interval sequence ----
# interval <- getIntervalTibble(omopTable = omopTable,
# start_date_name = date,
# end_date_name = date,
# unit = unit,
# unitInterval = unitInterval)
#
# # Insert interval table to the cdm ----
# cdm <- cdm |>
# omopgenerics::insertTable(name = "interval", table = interval)
#
# # Create summarised result ----
# splitIncidenceBetweenIntervals <- function(cdm, omopTable, date){
#
# result <- cdm$interval |>
# dplyr::cross_join(
# omopTable |>
# dplyr::rename("incidence_date" = dplyr::all_of(date))
# ) |>
# dplyr::filter(.data$incidence_date >= .data$interval_start_date &
# .data$incidence_date <= .data$interval_end_date) |>
# dplyr::group_by(.data$interval_group, dplyr::across(dplyr::any_of(strata))) |>
# dplyr::summarise("estimate_value" = dplyr::n(), .groups = "drop") |>
# dplyr::collect() |>
# dplyr::ungroup()
#
# }
#
#
# if(!is.null(strata)){
# result <- result |>
# rbind(
# result |>
# dplyr::group_by(.data$interval_group) |>
# dplyr::summarise(estimate_value = sum(.data$estimate_value), .groups = "drop") |>
# dplyr::mutate(age_group = "overall")
# ) |>
# dplyr::rename() |>
# dplyr::mutate()
# }else{
# result <- result |>
# dplyr::mutate("age_group" = "overall")
# }
#
# result <- result |>
# dplyr::mutate(
# "estimate_value" = as.character(.data$estimate_value),
# "variable_name" = "incidence_records",
# ) |>
# dplyr::rename("variable_level" = "interval_group") |>
# visOmopResults::uniteStrata(cols = "age_group") |>
# dplyr::mutate(
# "result_id" = as.integer(1),
# "cdm_name" = omopgenerics::cdmName(omopgenerics::cdmReference(omopTable)),
# "group_name" = "omop_table",
# "group_level" = name,
# "estimate_name" = "count",
# "estimate_type" = "integer",
# "additional_name" = "time_interval",
# "additional_level" = gsub(" to.*","",.data$variable_level)
# ) |>
# omopgenerics::newSummarisedResult(settings = dplyr::tibble(
# "result_id" = 1L,
# "result_type" = "summarised_table_counts",
# "package_name" = "OmopSketch",
# "package_version" = as.character(utils::packageVersion("OmopSketch")),
# "unit" = .env$unit,
# "unitInterval" = .env$unitInterval
# ))
#
# omopgenerics::dropTable(cdm = cdm, name = "interval")
#
# return(result)
}


Expand Down
4 changes: 2 additions & 2 deletions R/tableClinicalRecords.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Create a gt table from a summarised omop_table.
#'
#' @param summarisedClinicalRecords A summarised_result object with the output from summariseOmopTable().
#' @param summarisedClinicalRecords A summarised_result object with the output from summariseClinicalRecords().
#'
#' @return A gt object with the summarised data.
#'
Expand Down Expand Up @@ -50,7 +50,7 @@ tableClinicalRecords <- function(summarisedClinicalRecords) {
"median [IQR]" = "<median> [<q25> - <q75>]",
"mean (sd)" = "<mean> (<sd>)"
),
keepNotFormatted = FALSE
keepNotFormatted = TRUE
) |>
suppressMessages() |>
visOmopResults::formatHeader(header = "cdm_name") |>
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ We can characterise the drug_exposure table:

```{r}
library(OmopSketch)
result <- summariseOmopTable(cdm$drug_exposure)
result <- summariseClinicalRecords(cdm$drug_exposure)
result |> glimpse()
```

We can create a visualisation of this table:

```{r}
tableOmopTable(result)
tableClinicalRecords(result)
```
8 changes: 4 additions & 4 deletions man/summariseOmopTable.Rd → man/summariseClinicalRecords.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/summariseRecordCount.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/tableClinicalRecords.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions man/tableOmopTable.Rd

This file was deleted.

1 change: 0 additions & 1 deletion tests/testthat/test-summariseClinicalRecords.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,5 @@ test_that("tableClinicalRecords() works", {
expect_true(inherits(x,"gt_tbl"))
expect_warning(tableClinicalRecords(summariseClinicalRecords(cdm$death)))
expect_true(inherits(tableClinicalRecords(summariseClinicalRecords(cdm$death)),"gt_tbl"))

})

Loading

0 comments on commit 07f2baf

Please sign in to comment.