Skip to content

Commit a2ffd33

Browse files
committed
add latest tests
1 parent 6d4afde commit a2ffd33

File tree

2 files changed

+133
-84
lines changed

2 files changed

+133
-84
lines changed

R/runVisitContext.R

+43-11
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ getVisitContext <- function(connection = NULL,
5757

5858
if (!is.null(conceptIdTable)) {
5959

60-
createTablesql <- "IF OBJECT_ID('@unique_concept_id_table', 'U') IS NULL CREATE TABLE @unique_concept_id_table (concept_id BIGINT);"
61-
62-
DatabaseConnector::renderTranslateExecuteSql(
63-
connection = connection,
64-
sql = createTablesql,
65-
tempEmulationSchema = tempEmulationSchema,
66-
unique_concept_id_table = conceptIdTable,
67-
progressBar = FALSE,
68-
reportOverallTime = FALSE
69-
)
60+
# add concepts to #concept_ids if they don't already exist
61+
if (!tempTableExists(connection, "concept_ids")) {
62+
DatabaseConnector::renderTranslateExecuteSql(
63+
connection = connection,
64+
sql = "CREATE TABLE #concept_ids (concept_id BIGINT);",
65+
tempEmulationSchema = tempEmulationSchema,
66+
progressBar = FALSE,
67+
reportOverallTime = FALSE
68+
)
69+
}
7070

7171
sql <- "INSERT INTO @unique_concept_id_table (concept_id)
7272
SELECT DISTINCT visit_concept_id
@@ -106,6 +106,33 @@ getVisitContext <- function(connection = NULL,
106106
return(visitContext)
107107
}
108108

109+
110+
111+
#' runVisitContext
112+
#'
113+
#' @description
114+
#' Generates the `visit_context.csv` which contains the counts for the subjects by `cohort_id`,
115+
#' `visit_concept_id` and `visit_context`. The `visit_context` categorizes visit occurrences of
116+
#' subjects based on how each the start and end date of each visit related to the cohort start date
117+
#' to which each subject belongs. No output will be generated for cohorts with no subjects.If there
118+
#' is no cohort with subjects execution will halt and `visit_context.csv` will not be generated.
119+
#'
120+
#' @template Connection
121+
#' @template cohortDefinitionSet
122+
#' @template ExportFolder
123+
#' @param databaseId A short string for identifying the database (e.g. 'Synpuf').
124+
#' @template CohortDatabaseSchema
125+
#' @template CdmDatabaseSchema
126+
#' @template TempEmulationSchema
127+
#' @template CohortTable
128+
#' @template cdmVersion
129+
#' @template MinCellCount
130+
#' @template Incremental
131+
#' @param incrementalFolder If \code{incremental = TRUE}, specify a folder where records are kept
132+
#' of which cohort diagnostics has been executed. If not specified, a file named `incremental` will be created inside the
133+
#' \code{export_folder} directory.
134+
#'
135+
#' @export
109136
runVisitContext <- function(connection,
110137
cohortDefinitionSet,
111138
exportFolder,
@@ -118,7 +145,12 @@ runVisitContext <- function(connection,
118145
minCellCount,
119146
incremental,
120147
incrementalFolder = file.path(exportFolder, "incremental")){
121-
148+
149+
# Create export file if it doesn't exist
150+
if (!file.exists(gsub("/$", "", exportFolder))) {
151+
dir.create(exportFolder, recursive = TRUE)
152+
ParallelLogger::logInfo("Created export folder", exportFolder)
153+
}
122154

123155
if (incremental && !file.exists(incrementalFolder)) {
124156
# Create the file if it doesn't exist

0 commit comments

Comments
 (0)