From f18602a957f949847048a2b611ebc1b75ea78bf0 Mon Sep 17 00:00:00 2001 From: Jamie Gilbert Date: Thu, 4 Jan 2024 08:26:30 -0800 Subject: [PATCH] Mock cohort set function --- R/CohortDefinitionSet.R | 43 +++++++++++++++++++++++++++++++ man/getMockCohortDefinitionSet.Rd | 16 ++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 R/CohortDefinitionSet.R create mode 100644 man/getMockCohortDefinitionSet.Rd diff --git a/R/CohortDefinitionSet.R b/R/CohortDefinitionSet.R new file mode 100644 index 0000000..7833870 --- /dev/null +++ b/R/CohortDefinitionSet.R @@ -0,0 +1,43 @@ +.decodeBase64 <- function(x) { + rawToChar(base64enc::base64decode(x)) +} + +#' Get Mock Cohort definition set for subset of reward cohorts. +#' @description +#' Useful in other analysis steps with ohdsi packages +#' +#' @export +#' @param rewardConfig Reward configuration +#' @param cohortIds Cohort Identifiers +getMockCohortDefinitionSet <- function(rewardConfig, cohortIds) { + connection <- DatabaseConnector::connect(rewardConfig$connectionDetails) + on.exit(DatabaseConnector::disconnect(connection)) + + sql <- " + SELECT + cd.cohort_definition_id as cohort_id, + cd.cohort_definition_name as cohort_name, + COALESCE(ar.sql_definition, '') as sql, + COALESCE(ar.definition, '') as json, + CASE WHEN ar.definition IS NULL THEN 0 ELSE 1 END as has_json + FROM @schema.@cohort_definition cd + LEFT JOIN @schema.@atlas_cohort_reference ar ON cd.cohort_definition_id = ar.cohort_definition_id + WHERE cd.cohort_definition_id IN (@cohort_ids) + " + + res <- DatabaseConnector::renderTranslateQuerySql(connection, + sql, + cohort_ids = cohortIds, + schema = rewardConfig$resultsSchema, + atlas_cohort_reference = rewardConfig$referenceTables$atlasCohortReference, + cohort_definition = rewardConfig$referenceTables$cohortDefinition, + snakeCaseToCamelCase = TRUE) + + + res <- res %>% + dplyr::mutate(json = ifelse(.data$hasJson == 1, .decodeBase64(.data$json), "{}")) %>% + dplyr::mutate(sql = ifelse(.data$hasJson == 1, .decodeBase64(.data$sql), "")) %>% + dplyr::select(-"hasJson") + + return(res) +} \ No newline at end of file diff --git a/man/getMockCohortDefinitionSet.Rd b/man/getMockCohortDefinitionSet.Rd new file mode 100644 index 0000000..024ec53 --- /dev/null +++ b/man/getMockCohortDefinitionSet.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/CohortDefinitionSet.R +\name{getMockCohortDefinitionSet} +\alias{getMockCohortDefinitionSet} +\title{Get Mock Cohort definition set for subset of reward cohorts.} +\usage{ +getMockCohortDefinitionSet(rewardConfig, cohortIds) +} +\arguments{ +\item{rewardConfig}{Reward configuration} + +\item{cohortIds}{Cohort Identifiers} +} +\description{ +Useful in other analysis steps with ohdsi packages +}