|
| 1 | +start_time <- Sys.time() |
| 2 | +outputFolder <- here::here("Results") |
| 3 | + |
| 4 | +logfile <- file.path( paste0(outputFolder, |
| 5 | + "/log_addendum", dbName, "_", format(Sys.time(), "%d_%m_%Y_%H_%M_%S"),".txt" |
| 6 | +)) |
| 7 | + |
| 8 | +log_message <- function(message) { |
| 9 | + cat(paste(Sys.time(), "-", message, "\n"), file = logfile, append = TRUE) |
| 10 | + cli::cli_inform(paste(Sys.time(), "-", message, "\n")) |
| 11 | +} |
| 12 | + |
| 13 | +log_message("Start time recorded") |
| 14 | + |
| 15 | + |
| 16 | +sex <- TRUE |
| 17 | +ageGroup <- list(c(0,19), c(20, 39),c(40, 59), c(60, 79), c(80, Inf)) |
| 18 | +ageGroup <- omopgenerics::validateAgeGroupArgument(ageGroup, ageGroupName = "")[[1]] |
| 19 | +dateRange <- as.Date(c("2012-01-01", NA)) |
| 20 | + |
| 21 | +log_message("Getting population characteristics") |
| 22 | + |
| 23 | + |
| 24 | +cdm <- omopgenerics::bind( |
| 25 | + CohortConstructor::demographicsCohort(cdm, "population_1", sex = "Both"), |
| 26 | + CohortConstructor::demographicsCohort(cdm, "population_2", sex = "Both", ageRange = ageGroup), |
| 27 | + name = "population" |
| 28 | +) |
| 29 | + |
| 30 | +omopgenerics::dropSourceTable(cdm = cdm, dplyr::starts_with("population_1")) |
| 31 | +omopgenerics::dropSourceTable(cdm = cdm, dplyr::starts_with("population_2")) |
| 32 | + |
| 33 | + |
| 34 | +set <- omopgenerics::settings(cdm$population) |> |
| 35 | + dplyr::mutate(cohort_name = tolower(dplyr::if_else( |
| 36 | + is.na(.data$age_range), "general_population", paste0("age_group_", .data$age_range) |
| 37 | + ))) |> |
| 38 | + dplyr::select("cohort_definition_id", "cohort_name") |
| 39 | + |
| 40 | +result_populationCharacteristics <- cdm$population |> |
| 41 | + omopgenerics::newCohortTable(cohortSetRef = set, .softValidation = TRUE) |> |
| 42 | + CohortConstructor::trimToDateRange(dateRange = dateRange) |> |
| 43 | + PatientProfiles::addSexQuery() |> |
| 44 | + CohortCharacteristics::summariseCharacteristics( |
| 45 | + strata = list("sex"), |
| 46 | + estimates = list( |
| 47 | + date = c("min", "q25", "median", "q75", "max"), |
| 48 | + numeric = c("min", "q25", "median", "q75", "max", "mean", "sd", "density"), |
| 49 | + categorical = c("count", "percentage"), |
| 50 | + binary = c("count", "percentage") |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | +log_message("Summarising in observation records and person-days") |
| 55 | + |
| 56 | +result_inObservation <- OmopSketch::summariseInObservation(cdm$observation_period, |
| 57 | + output = c("records","person-days"), |
| 58 | + interval = "years", |
| 59 | + sex = sex, |
| 60 | + ageGroup = ageGroup, |
| 61 | + dateRange = dateRange) |
| 62 | +log_message("Summarising missing data - person table") |
| 63 | + |
| 64 | +result_missingDataPerson <- OmopSketch::summariseMissingData(cdm, |
| 65 | + omopTableName = "person") |
| 66 | + |
| 67 | +result <- omopgenerics::bind(result_populationCharacteristics, result_inObservation, result_missingDataPerson) |
| 68 | +omopgenerics::exportSummarisedResult(result, minCellCount = minCellCount, path = outputFolder, fileName = "result_addendum_{cdm_name}.csv") |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +# Calculate duration and log |
| 73 | +dur <- abs(as.numeric(Sys.time() - start_time, units = "secs")) |
| 74 | +log_message(paste("Study code finished. Code ran in", floor(dur / 60), "min and", dur %% 60 %/% 1, "sec")) |
| 75 | + |
| 76 | +# Close connection |
| 77 | +CDMConnector::cdmDisconnect(cdm) |
| 78 | +log_message("Database connection closed") |
| 79 | + |
| 80 | +# Zip the results |
| 81 | +log_message("Zipping results") |
| 82 | + |
| 83 | +files_to_zip <- list.files(outputFolder) |
| 84 | +files_to_zip <- files_to_zip[stringr::str_detect(files_to_zip, "addendum")] |
| 85 | + |
| 86 | +zip::zip(zipfile = file.path(paste0( |
| 87 | + outputFolder, "/results_addendum_", dbName, ".zip" |
| 88 | +)), |
| 89 | +files = files_to_zip, |
| 90 | +root = outputFolder) |
0 commit comments