extras/exampleCodeToRun/CodeToRun_Other.R

# connection details ----
connectionSpecifications <- cdmSources %>%
  dplyr::filter(sequence == 1) %>%
  dplyr::filter(database == 'truven_mdcd')

connectionDetails <-
  DatabaseConnector::createConnectionDetails(
    dbms = connectionSpecifications$dbms,
    user = keyring::key_get(service = userNameService),
    password = keyring::key_get(service = passwordService),
    port = connectionSpecifications$port,
    server = connectionSpecifications$server
  )

cdmSourceTable <-
  DatabaseConnector::renderTranslateQuerySql(
    connection = DatabaseConnector::connect(connectionDetails),
    sql = "SELECT * FROM @cdm_database_schema.cdm_source;",
    cdm_database_schema = connectionSpecifications$cdmDatabaseSchema
  ) %>%
  dplyr::tibble()

databaseId = cdmSourceTable$CDM_SOURCE_ABBREVIATION
databaseName = cdmSourceTable$CDM_SOURCE_NAME
databaseDescription = cdmSourceTable$SOURCE_DESCRIPTION
cdmDatabaseSchema = connectionSpecifications$cdmDatabaseSchema
vocabularyDatabaseSchema = connectionSpecifications$vocabDatabaseSchema
cohortDatabaseSchema = connectionSpecifications$cohortDatabaseSchema
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema")


# Cohort Definitions ----
remotes::install_github('OHDSI/SkeletonCohortDiagnosticsStudy', ref = "develop")
studyName <- 'epi999'
## get cohort definition set ----
cohortDefinitionSet <-
  CohortGenerator::getCohortDefinitionSet(
    settingsFileName = "settings/CohortsToCreate.csv",
    jsonFolder = "cohorts",
    sqlFolder = "sql/sql_server",
    packageName = "SkeletonCohortDiagnosticsStudy",
    cohortFileNameValue = "cohortId"
  ) %>%  dplyr::tibble()
cohortTableNames = CohortGenerator::getCohortTableNames(cohortTable =
                                                          paste0("s", studyName, "_", connectionSpecifications$sourceId))

# output folder information ----
outputFolder <-
  file.path("D:", "temp", "outputFolder", studyName, connectionSpecifications$database)
## optionally delete previous execution ----
unlink(x = outputFolder,
       recursive = TRUE,
       force = TRUE)
dir.create(path = outputFolder,
           showWarnings = FALSE,
           recursive = TRUE)

# Execution ----
## Create cohort tables on remote ----
CohortGenerator::createCohortTables(connectionDetails = connectionDetails, 
                                    cohortDatabaseSchema = cohortDatabaseSchema, 
                                    cohortTableNames = cohortTableNames, 
                                    incremental = TRUE)
## Generate cohort on remote ----
CohortGenerator::generateCohortSet(connectionDetails = connectionDetails, 
                                   cdmDatabaseSchema = cdmDatabaseSchema, 
                                   tempEmulationSchema = tempEmulationSchema, 
                                   cohortTableNames = cohortTableNames, 
                                   cohortDefinitionSet = cohortDefinitionSet, 
                                   cohortDatabaseSchema = cohortDatabaseSchema, 
                                   incremental = TRUE, 
                                   incrementalFolder = file.path(outputFolder, "incremental"))

## Execute Cohort Diagnostics on remote ----
CohortDiagnostics::executeDiagnostics(
  cohortDefinitionSet = cohortDefinitionSet,
  exportFolder = outputFolder,
  databaseId = databaseId,
  databaseName = databaseName,
  databaseDescription = databaseDescription,
  cohortDatabaseSchema = cohortDatabaseSchema,
  cdmDatabaseSchema = cdmDatabaseSchema,
  tempEmulationSchema = tempEmulationSchema,
  connectionDetails = connectionDetails,
  cohortTableNames = cohortTableNames,
  vocabularyDatabaseSchema = vocabularyDatabaseSchema,
  incremental = TRUE
)


# example of how to run full time series diagnostics outside executeDiagnostics
data <-
  CohortDiagnostics::runCohortTimeSeriesDiagnostics(
    connectionDetails = connectionDetails,
    cdmDatabaseSchema = cdmDatabaseSchema,
    cohortDatabaseSchema = cohortDatabaseSchema,
    cohortTable = cohortTableNames$cohortTable,
    runCohortTimeSeries = TRUE,
    runDataSourceTimeSeries = TRUE
  )
# to export data into csv in Cohort Diagnostics compatible form
data <- CohortDiagnostics:::makeDataExportable(x = data,
                                               tableName = "time_series",
                                               databaseId = databaseId)
CohortDiagnostics:::writeToCsv(
  data = data,
  fileName = file.path(outputFolder, "time_series.csv"),
  incremental = FALSE,
  cohortId = data$cohortId %>% unique()
)


# package results ----
CohortDiagnostics::createMergedResultsFile(dataFolder = outputFolder, overwrite = TRUE)
# Launch diagnostics explorer shiny app ----
CohortDiagnostics::launchDiagnosticsExplorer()


# upload to postgres db ----
# connectionDetailsToUpload <- createConnectionDetails(dbms = "postgresql",
#                                                      server = paste(Sys.getenv("shinydbServer"),
#                                                                     Sys.getenv("shinydbDatabase"),
#                                                                     sep = "/"),
#                                                      port = Sys.getenv("shinydbPort"),
#                                                      user = Sys.getenv("shinyDbUser"),
#                                                      password = Sys.getenv("shinydbPw"))
# resultsSchema <- "eunomiaCd"
# CohortDiagnostics::createResultsDataModel(connectionDetails = connectionDetailsToUpload, 
#                                           schema = resultsSchema)
# zipFilesToUpload <- list.files(path = outputFolder,
#                                pattern = ".zip",
#                                recursive = TRUE,
#                                full.names = TRUE)
# 
# for (i in (1:length(zipFilesToUpload))) {
#   CohortDiagnostics::uploadResults(connectionDetails = connectionDetailsToUpload,
#                                    schema = resultsSchema,
#                                    zipFileName = zipFilesToUpload[[i]])
# }
# 
# CohortDiagnostics::launchDiagnosticsExplorer(connectionDetails = connectionDetailsToUpload, 
#                                              resultsDatabaseSchema = resultsSchema)
OHDSI/CohortDiagnostics documentation built on Oct. 9, 2024, 5:46 a.m.