Skip to content

Commit

Permalink
Merge pull request #20 from OHDSI/shiny_app
Browse files Browse the repository at this point in the history
Shiny app
  • Loading branch information
jreps authored Mar 14, 2023
2 parents db30a95 + e848a05 commit 4b9a1c1
Show file tree
Hide file tree
Showing 9 changed files with 435 additions and 9 deletions.
8 changes: 7 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ Imports:
readr,
rlang
Suggests:
devtools,
testthat,
Eunomia,
kableExtra,
knitr,
markdown,
ResultModelManager,
ShinyAppBuilder,
shiny,
withr
Remotes:
ohdsi/FeatureExtraction,
ohdsi/Eunomia
ohdsi/Eunomia,
ohdsi/ResultModelManager,
ohdsi/ShinyAppBuilder
NeedsCompilation: no
RoxygenNote: 7.2.3
Encoding: UTF-8
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ export(saveCharacterizationSettings)
export(saveDechallengeRechallengeAnalyses)
export(saveRechallengeFailCaseSeriesAnalyses)
export(saveTimeToEventAnalyses)
export(viewCharacterization)
importFrom(dplyr,"%>%")
importFrom(rlang,.data)
1 change: 0 additions & 1 deletion R/Characterization.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
"_PACKAGE"

#' @importFrom rlang .data
#' @importFrom methods is
#' @importFrom dplyr %>%
NULL
8 changes: 4 additions & 4 deletions R/HelperFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
connectionDetails,
errorMessages
) {
if (is(connectionDetails, "connectionDetails")) {
if (inherits(connectionDetails, "connectionDetails")) {
checkmate::assertClass(
x = connectionDetails,
classes = "connectionDetails",
Expand Down Expand Up @@ -65,7 +65,7 @@
return()
}

if(class(settings) == 'dechallengeRechallengeSettings'){
if(inherits(settings, 'dechallengeRechallengeSettings')){
settings <- list(settings)
}

Expand Down Expand Up @@ -99,7 +99,7 @@
return()
}

if(class(settings) == 'timeToEventSettings'){
if(inherits(settings,'timeToEventSettings')){
settings <- list(settings)
}

Expand Down Expand Up @@ -133,7 +133,7 @@
return()
}

if(class(settings) == 'aggregateCovariateSettings'){
if(inherits(settings,'aggregateCovariateSettings')){
settings <- list(settings)
}

Expand Down
6 changes: 3 additions & 3 deletions R/RunCharacterization.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ createCharacterizationSettings <- function(
errorMessages = errorMessages
)

if (class(timeToEventSettings) == "timeToEventSettings") {
if (inherits(timeToEventSettings, "timeToEventSettings")) {
timeToEventSettings <- list(timeToEventSettings)
}
if (class(dechallengeRechallengeSettings) == "dechallengeRechallengeSettings") {
if (inherits(dechallengeRechallengeSettings, "dechallengeRechallengeSettings")) {
dechallengeRechallengeSettings <- list(dechallengeRechallengeSettings)
}
if (class(aggregateCovariateSettings) == "aggregateCovariateSettings") {
if (inherits(aggregateCovariateSettings, "aggregateCovariateSettings")) {
aggregateCovariateSettings <- list(aggregateCovariateSettings)
}

Expand Down
217 changes: 217 additions & 0 deletions R/ViewShiny.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#' viewCharacterization - Interactively view the characterization results
#'
#' @description
#' This is a shiny app for viewing interactive plots and tables
#' @details
#' Input is the output of ...
#' @param resultLocation The location of the results
#' @param cohortDefinitionSet The cohortDefinitionSet extracted using webAPI
#' @return
#' Opens a shiny app for interactively viewing the results
#'
#' @export
viewCharacterization <- function(
resultLocation,
cohortDefinitionSet = NULL
) {

databaseSettings <- prepareCharacterizationShiny(
resultLocation = resultLocation,
cohortDefinitionSet = cohortDefinitionSet
)

viewChars(databaseSettings)
}

prepareCharacterizationShiny <- function(
resultLocation,
cohortDefinitionSet
){
server <- file.path(resultLocation, 'sqliteCharacterization', 'sqlite.sqlite')

connectionDetailsSettings <- list(
dbms = 'sqlite',
server = server
)

connectionDetails <- do.call(
what = DatabaseConnector::createConnectionDetails,
args = connectionDetailsSettings
)

con <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(con))

tables <- tolower(DatabaseConnector::getTableNames(con, 'main'))

if(!'cg_cohort_definition' %in% tables){
cohortIds <- unique(
c(
DatabaseConnector::querySql(con, 'select distinct TARGET_COHORT_ID from c_cohort_details where TARGET_COHORT_ID != 0;')$TARGET_COHORT_ID,
DatabaseConnector::querySql(con, 'select distinct OUTCOME_COHORT_ID from c_cohort_details where OUTCOME_COHORT_ID != 0;')$OUTCOME_COHORT_ID,
DatabaseConnector::querySql(con, 'select distinct TARGET_COHORT_DEFINITION_ID from c_time_to_event;')$TARGET_COHORT_DEFINITION_ID,
DatabaseConnector::querySql(con, 'select distinct OUTCOME_COHORT_DEFINITION_ID from c_time_to_event;')$OUTCOME_COHORT_DEFINITION_ID,
DatabaseConnector::querySql(con, 'select distinct TARGET_COHORT_DEFINITION_ID from c_rechallenge_fail_case_series;')$TARGET_COHORT_DEFINITION_ID,
DatabaseConnector::querySql(con, 'select distinct OUTCOME_COHORT_DEFINITION_ID from c_rechallenge_fail_case_series;')$OUTCOME_COHORT_DEFINITION_ID
)
)

DatabaseConnector::insertTable(
connection = con,
databaseSchema = 'main',
tableName = 'cg_COHORT_DEFINITION',
data = data.frame(
cohortDefinitionId = cohortIds,
cohortName = getCohortNames(cohortIds, cohortDefinitionSet)
),
camelCaseToSnakeCase = T
)
}

if(!'database_meta_data' %in% tables){
dbIds <- unique(
c(
DatabaseConnector::querySql(con, 'select distinct DATABASE_ID from c_analysis_ref;')$DATABASE_ID,
DatabaseConnector::querySql(con, 'select distinct DATABASE_ID from c_dechallenge_rechallenge;')$DATABASE_ID,
DatabaseConnector::querySql(con, 'select distinct DATABASE_ID from c_time_to_event;')$DATABASE_ID
)
)

DatabaseConnector::insertTable(
connection = con,
databaseSchema = 'main',
tableName = 'DATABASE_META_DATA',
data = data.frame(
databaseId = dbIds,
cdmSourceAbbreviation = paste0('database ', dbIds)
),
camelCaseToSnakeCase = T
)
}

if(!'i_incidence_summary' %in% tables){

x <- c("refId", "databaseId", "sourceName",
"targetCohortDefinitionId", "targetName", "tarId",
"tarStartWith", "tarStartOffset", "tarEndWith", "tarEndOffset",
"subgroupId", 'subgroupName',
'outcomeId','outcomeCohortDefinitionId', 'outcomeName',
'clean_window',
'ageId', 'ageGroupName',
'genderId', 'genderName',
'startYear', 'personsAtRiskPe', 'personsAtRisk',
'personDaysPe', 'personDays',
'personOutcomesPe', 'personOutcomes',
'outcomesPe', 'outcomes',
'incidenceProportionP100p',
'incidenceRateP100py'
)
df <- data.frame(matrix(ncol = length(x), nrow = 0))
colnames(df) <- x

DatabaseConnector::insertTable(
connection = con,
databaseSchema = 'main',
tableName = 'i_incidence_summary',
data = df,
camelCaseToSnakeCase = T
)
}

databaseSettings <- list(
connectionDetailsSettings = connectionDetailsSettings,
schema = 'main',
tablePrefix = 'c_',
cohortTablePrefix = 'cg_',
incidenceTablePrefix = 'i_',
databaseTable = 'DATABASE_META_DATA'
)

return(databaseSettings)
}

viewChars <- function(
databaseSettings,
testApp = F
){
ensure_installed("ShinyAppBuilder")
ensure_installed("ResultModelManager")

connectionDetails <- do.call(
DatabaseConnector::createConnectionDetails,
databaseSettings$connectionDetailsSettings
)
connection <- ResultModelManager::ConnectionHandler$new(connectionDetails)
databaseSettings$connectionDetailsSettings <- NULL

# set database settings into system variables
Sys.setenv("resultDatabaseDetails_characterization" = as.character(ParallelLogger::convertSettingsToJson(databaseSettings)))

config <- ParallelLogger::loadSettingsFromJson(
fileName = system.file(
'shinyConfig.json',
package = "Characterization"
)
)

if(!testApp){
ShinyAppBuilder::viewShiny(config = config, connection = connection)
} else{
ShinyAppBuilder::createShinyApp(config = config, connection = connection)
}
}



getCohortNames <- function(cohortIds, cohortDefinitionSet){

if(!is.null(cohortDefinitionSet)){
cohortNames <- sapply(
cohortIds,
function(x){
cohortDefinitionSet$cohortName[cohortDefinitionSet$cohortId == x]
}
)
} else{
cohortNames <- paste0('cohort ', cohortIds)
}

return(cohortNames)
}


# Borrowed from devtools: https://github.com/hadley/devtools/blob/ba7a5a4abd8258c52cb156e7b26bb4bf47a79f0b/R/utils.r#L44
is_installed <- function (pkg, version = 0) {
installed_version <- tryCatch(utils::packageVersion(pkg),
error = function(e) NA)
!is.na(installed_version) && installed_version >= version
}

# Borrowed and adapted from devtools: https://github.com/hadley/devtools/blob/ba7a5a4abd8258c52cb156e7b26bb4bf47a79f0b/R/utils.r#L74
ensure_installed <- function(pkg) {
if (!is_installed(pkg)) {
msg <- paste0(sQuote(pkg), " must be installed for this functionality.")
if (interactive()) {
message(msg, "\nWould you like to install it?")
if (utils::menu(c("Yes", "No")) == 1) {
if(pkg%in%c("ShinyAppBuilder", "ResultModelManager")){

# add code to check for devtools...
dvtCheck <- tryCatch(utils::packageVersion('devtools'),
error = function(e) NA)
if(is.na(dvtCheck)){
utils::install.packages('devtools')
}

devtools::install_github(paste0('OHDSI/',pkg))
}else{
utils::install.packages(pkg)
}
} else {
stop(msg, call. = FALSE)
}
} else {
stop(msg, call. = FALSE)
}
}
}
32 changes: 32 additions & 0 deletions inst/shinyConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"shinyModules": [
{
"id": "about",
"tabName": "About",
"tabText": "About",
"shinyModulePackage": "OhdsiShinyModules",
"uiFunction": "aboutViewer",
"serverFunction": "aboutServer",
"databaseConnectionKeyService": null,
"databaseConnectionKeyUsername": null,
"infoBoxFile": "aboutHelperFile()",
"icon": "info",
"keyring": true,
"order": 1
},
{
"id": "characterization",
"tabName": "Characterization",
"tabText": "Characterization",
"shinyModulePackage": "OhdsiShinyModules",
"uiFunction": "descriptionViewer",
"serverFunction": "descriptionServer",
"databaseConnectionKeyService": "resultDatabaseDetails",
"databaseConnectionKeyUsername": "characterization",
"infoBoxFile": "descriptionHelperFile()",
"icon": "chart-line",
"keyring": false,
"order": 2
}
]
}
22 changes: 22 additions & 0 deletions man/viewCharacterization.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4b9a1c1

Please sign in to comment.