forked from thehyve/ShinyDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.R
163 lines (143 loc) · 6.78 KB
/
global.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
library(magrittr)
source("R/Tables.R")
source("R/Plots.R")
source("R/Results.R")
# shinySettings <- list(connectionDetails = DatabaseConnector::createConnectionDetails(dbms = "postgresql",
# server = "localhost/ohdsi",
# user = "postgres",
# password = Sys.getenv("pwPostgres")),
# resultsDatabaseSchema = "phenotype_library",
# vocabularyDatabaseSchema = "phenotype_library")
# shinySettings <- list(dataFolder = "s:/examplePackageOutput")
# Settings when running on server:
defaultLocalDataFolder <- "data"
defaultLocalDataFile <- "PreMerged.RData"
connectionPool <- NULL
defaultServer <- Sys.getenv("phenotypeLibraryDbServer")
defaultDatabase <- Sys.getenv("phenotypeLibraryDbDatabase")
defaultPort <- Sys.getenv("phenotypeLibraryDbPort")
defaultUser <- Sys.getenv("phenotypeLibraryDbUser")
defaultPassword <- Sys.getenv("phenotypeLibraryDbPassword")
defaultResultsSchema <- Sys.getenv("phenotypeLibraryDbResultsSchema")
defaultVocabularySchema <- Sys.getenv("phenotypeLibraryDbVocabularySchema")
defaultDatabaseMode <- FALSE # Use file system if FALSE
defaultCohortBaseUrl <- "https://atlas.ohdsi.org/#/cohortdefinition/"
defaultConceptBaseUrl <- "https://athena.ohdsi.org/search-terms/terms/"
cohortDiagnosticModeDefaultTitle <- "Cohort Diagnostics"
phenotypeLibraryModeDefaultTitle <- "Phenotype Library"
if (!exists("shinySettings")) {
writeLines("Using default settings")
databaseMode <- defaultDatabaseMode & defaultServer != ""
if (databaseMode) {
connectionPool <- pool::dbPool(
drv = DatabaseConnector::DatabaseConnectorDriver(),
dbms = "postgresql",
server = paste(defaultServer, defaultDatabase, sep = "/"),
port = defaultPort,
user = defaultUser,
password = defaultPassword
)
resultsDatabaseSchema <- defaultResultsSchema
vocabularyDatabaseSchema <- defaultVocabularySchema
} else {
dataFolder <- defaultLocalDataFolder
}
cohortBaseUrl <- defaultCohortBaseUrl
conceptBaseUrl <- defaultCohortBaseUrl
} else {
writeLines("Using settings provided by user")
databaseMode <- !is.null(shinySettings$connectionDetails)
if (databaseMode) {
connectionDetails <- shinySettings$connectionDetails
if (is(connectionDetails$server, "function")) {
connectionPool <- pool::dbPool(drv = DatabaseConnector::DatabaseConnectorDriver(),
dbms = "postgresql",
server = connectionDetails$server(),
port = connectionDetails$port(),
user = connectionDetails$user(),
password = connectionDetails$password(),
connectionString = connectionDetails$connectionString())
} else {
# For backwards compatibility with older versions of DatabaseConnector:
connectionPool <- pool::dbPool(drv = DatabaseConnector::DatabaseConnectorDriver(),
dbms = "postgresql",
server = connectionDetails$server,
port = connectionDetails$port,
user = connectionDetails$user,
password = connectionDetails$password,
connectionString = connectionDetails$connectionString)
}
resultsDatabaseSchema <- shinySettings$resultsDatabaseSchema
vocabularyDatabaseSchema <- shinySettings$vocabularyDatabaseSchema
} else {
dataFolder <- shinySettings$dataFolder
}
cohortBaseUrl <- shinySettings$cohortBaseUrl
conceptBaseUrl <- shinySettings$cohortBaseUrl
}
dataModelSpecifications <- read.csv("resultsDataModelSpecification.csv")
# Cleaning up any tables in memory:
suppressWarnings(rm(list = SqlRender::snakeCaseToCamelCase(dataModelSpecifications$tableName)))
if (databaseMode) {
onStop(function() {
if (DBI::dbIsValid(connectionPool)) {
writeLines("Closing database pool")
pool::poolClose(connectionPool)
}
})
resultsTablesOnServer <- tolower(DatabaseConnector::dbListTables(connectionPool, schema = resultsDatabaseSchema))
loadResultsTable <- function(tableName, required = FALSE) {
if (required || tableName %in% resultsTablesOnServer) {
tryCatch({
table <- DatabaseConnector::dbReadTable(connectionPool,
paste(resultsDatabaseSchema, tableName, sep = "."))
}, error = function(err) {
stop("Error reading from ", paste(resultsDatabaseSchema, tableName, sep = "."), ": ", err$message)
})
colnames(table) <- SqlRender::snakeCaseToCamelCase(colnames(table))
if (nrow(table) > 0) {
assign(SqlRender::snakeCaseToCamelCase(tableName), dplyr::as_tibble(table), envir = .GlobalEnv)
}
}
}
loadResultsTable("database", required = TRUE)
loadResultsTable("cohort", required = TRUE)
loadResultsTable("phenotype_description")
loadResultsTable("temporal_time_ref")
loadResultsTable("concept_sets")
# Create empty objects in memory for all other tables. This is used by the Shiny app to decide what tabs to show:
isEmpty <- function(tableName) {
sql <- sprintf("SELECT 1 FROM %s.%s LIMIT 1;", resultsDatabaseSchema, tableName)
oneRow <- DatabaseConnector::dbGetQuery(connectionPool, sql)
return(nrow(oneRow) == 0)
}
for (table in dataModelSpecifications$tableName) {
if (table %in% resultsTablesOnServer &&
!exists(SqlRender::snakeCaseToCamelCase(table)) &&
!isEmpty(table)) {
assign(SqlRender::snakeCaseToCamelCase(table), dplyr::tibble())
}
}
dataSource <- createDatabaseDataSource(connection = connectionPool,
resultsDatabaseSchema = resultsDatabaseSchema,
vocabularyDatabaseSchema = vocabularyDatabaseSchema)
} else {
localDataPath <- file.path(dataFolder, defaultLocalDataFile)
if (!file.exists(localDataPath)) {
stop(sprintf("Local data file %s does not exist.", localDataPath))
}
dataSource <- createFileDataSource(localDataPath, envir = .GlobalEnv)
}
if (exists("temporalTimeRef")) {
temporalCovariateChoices <- temporalTimeRef %>%
dplyr::mutate(choices = paste0("Start ", .data$startDay, " to end ", .data$endDay)) %>%
dplyr::select(.data$timeId, .data$choices) %>%
dplyr::arrange(.data$timeId) %>%
dplyr::slice_head(n = 5)
}
if (exists("covariateRef")) {
specifications <- readr::read_csv(file = "Table1Specs.csv",
col_types = readr::cols(),
guess_max = min(1e7))
prettyAnalysisIds <- specifications$analysisId
}