Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shiny in inst #100

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ Suggests:
gt,
omock,
OmopSketch,
OmopViewer,
testthat (>= 3.0.0),
knitr,
visOmopResults,
glue,
RPostgres,
PatientProfiles,
ggplot2,
ggpubr,
stringr,
shiny,
DiagrammeR,
sortable
sortable,
shinycssloaders,
here,
DT,
bslib,
shinyWidgets,
plotly,
tidyr
Config/testthat/edition: 3
RoxygenNote: 7.3.2
Imports:
Expand All @@ -42,15 +50,12 @@ Imports:
CohortConstructor,
cli,
dplyr,
here,
IncidencePrevalence (>= 0.8.0),
omopgenerics,
magrittr,
purrr,
rmarkdown,
rlang,
vctrs,
visOmopResults,
glue
usethis,
vctrs
URL: https://ohdsi.github.io/PhenotypeR/
VignetteBuilder: knitr
118 changes: 16 additions & 102 deletions R/shinyDiagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#'
#' @inheritParams resultDoc
#' @inheritParams directoryDoc
#' @param open If TRUE, the shiny app will be launched in a new session. If
#' FALSE, the shiny app will be created but not launched.
#'
#' @return A shiny app
#' @export
Expand Down Expand Up @@ -50,111 +52,23 @@
#' shinyDiagnostics(my_result_cohort_diag, here::here())
#' }
shinyDiagnostics <- function(result,
directory){
directory,
open = rlang::is_interactive()){

rlang::check_installed("OmopViewer")
file.copy(from = system.file("shiny",
package = "PhenotypeR"),
to = directory,
recursive = TRUE,
overwrite = TRUE)

result |>
OmopViewer::exportStaticApp(
directory = directory,
# background = getBackground(result),
summary = TRUE,
panels = list(
"Database details" = c("Snapshot"= "summarise_omop_snapshot",
"Observation periods"= "summarise_observation_period"),
"Codelist diagnostics" = c(
"Achilles code use" = "achilles_code_use",
"Cohort code use" = "cohort_code_use",
"Orphan code use" = "orphan_code_use"),
"Cohort diagnostics" = c(
"Cohort characteristics" = "summarise_characteristics",
"Cohort attrition" = "summarise_cohort_attrition",
"Cohort overlap" = "summarise_cohort_overlap",
"Cohort timing" = "summarise_cohort_timing"),
"Matched diagnostics" = c(
"Large scale characteristics" = "summarise_large_scale_characteristics"),
"Population diagnostics" = c(
"Incidence" = "incidence",
"Incidence attrition" = "incidence_attrition",
"Prevalence" = "prevalence",
"Prevalence attrition" = "prevalence_attrition")
)
)

# replace md background file
file.copy(from = system.file("extdata",
"background.md",
package = "PhenotypeR"),
to = file.path(directory, "shiny",
"background.md"),
overwrite = TRUE)

}

getBackground <- function(result) {

cohorts <- result |>
visOmopResults::filterSettings(.data$table_name == "my_cohort") |>
dplyr::distinct(.data$group_name, .data$group_level) |>
visOmopResults::splitGroup()

if ("cohort_name" %in% colnames(cohorts)) {
cohorts <- c(
"title" = "**Cohorts**",
"body" = "The diagnostic results cover the following cohorts: {paste0(unique(cohorts$cohort_name), collapse = ', ')}" |> glue::glue()
)
} else {
cohorts <- character()
}

codelists <- result |>
visOmopResults::filterSettings(.data$result_type == "cohort_code_use") |>
dplyr::distinct(.data$group_name, .data$group_level) |>
visOmopResults::splitGroup()

if ("codelist_name" %in% colnames(codelists)) {
codelists <- c(
"title" = "**Codelists**",
"body" = "Diagnostics have been generated for these codelists: {paste0(unique(codelists$codelist_name), collapse = ', ')}" |> glue::glue()
)
} else {
codelists <- character()
omopgenerics::exportSummarisedResult(result,
fileName = "result.csv",
path = file.path(directory, "shiny", "data", "raw"))
# shiny::shinyAppDir(file.path(directory, "shiny"))
if (isTRUE(open)) {
usethis::proj_activate(directory)
}

databases <- result |>
dplyr::filter(!is.na(.data$cdm_name)) |>
dplyr::pull("cdm_name") |>
unique()

if (length(databases) > 0) {
databases <- c(
"title" = "**Databases**",
"body" = "The results are based on data from the following databases: {paste0(databases, collapse = ', ')}" |> glue::glue()
)
} else {
databases <- character()
}

resTypes <- settings(result) |>
dplyr::filter(!is.na(.data$result_type)) |>
dplyr::pull("result_type") |>
unique()

if (length(resTypes) > 0) {
resTypes <- c(
"title" = "**Results**",
"body" = "The following results are available: {paste0(resTypes, collapse = ', ')}" |> glue::glue()
)
} else {
resTypes <- character()
}
return(invisible())

c(
"header" = "PhenotypeR Diagnostics",
cohorts,
codelists,
databases,
resTypes,
"footer" = "This Shiny App presents results generated using the [PhenotypeR](https://ohdsi.github.io/PhenotypeR/) package (version {as.character(utils::packageVersion('PhenotypeR'))})." |> glue::glue()
)
}
3 changes: 0 additions & 3 deletions inst/extdata/background.md

This file was deleted.

19 changes: 19 additions & 0 deletions inst/shiny/background.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This shiny app presents the results from PhenotypeR

The PhenotypeR package helps us to assess the research-readiness of a set of cohorts we have defined. This assessment includes:

- ___Database diagnostics___ which help us to better understand the database in which they have been created. This includes information about the size of the data, the time period covered, the number of people in the data as a whole. More granular information that may influence analytic decisions, such as the number of observation periods per person, is also described.
- ___Codelist diagnostics___ which help to answer questions like what concepts from our codelist are used in the database? What concepts were present led to individuals' entry in the cohort? Are there any concepts being used in the database that we didn't include in our codelist but maybe we should have?
- ___Cohort diagnostics___ which help to answer questions like how many individuals did we include in our cohort and how many were excluded because of our inclusion criteria? If we have multiple cohorts, is there overlap between them and when do people enter one cohort relative to another? What is the incidence of cohort entry and what is the prevalence of the cohort in the database?
- ___Matched diagnostics___ which compares our study cohorts to the overall population in the database. By matching people in the cohorts to people with a similar age and sex in the database we can see how our cohorts differ from the general database population.
- ___Population diagnostics___ which estimates the frequency of our study cohorts in the database in terms of their incidence rates and prevalence..

<div style="display: flex; align-items: center; justify-content: flex-end; gap: 10px;">
<a href="https://github.com/OHDSI/PhenotypeR" target="_blank">
<img src="phenotyper_logo.png" alt="PhenotypeR Logo" style="width: 100px;">
</a>
<a href="https://github.com/OHDSI/PhenotypeR" target="_blank">
<img src="ohdsi_logo.svg" alt="OHDSI Logo" style="width: 100px;">
</a>
</div>

Empty file added inst/shiny/data/raw/readme.md
Empty file.
Empty file added inst/shiny/data/readme.md
Empty file.
2 changes: 2 additions & 0 deletions inst/shiny/functions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


52 changes: 52 additions & 0 deletions inst/shiny/global.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
library(bslib)
library(CodelistGenerator)
library(CohortCharacteristics)
library(DiagrammeR)
library(dplyr)
library(DT)
library(ggplot2)
library(gt)
library(here)
library(IncidencePrevalence)
library(OmopSketch)
library(readr)
library(shiny)
library(sortable)
library(visOmopResults)
library(shinycssloaders)
library(shinyWidgets)
library(plotly)
library(tidyr)

source(file.path(getwd(),"scripts", "functions.R"))

if(file.exists(file.path(getwd(), "data", "appData.RData"))){
load(file.path(getwd(),"data", "appData.RData"))
} else {
source(file.path(getwd(),"scripts", "preprocess.R"))
}

plotComparedLsc <- function(lsc, cohorts){
lsc <- lsc |> tidy()
plot_data <- lsc |>
filter(cohort_name %in% c(cohorts
)) |>
select(cohort_name,
variable_name,
variable_level,
percentage) |>
pivot_wider(names_from = cohort_name,
values_from = percentage)

plot <- plot_data |>
ggplot(aes(text = paste("Label:", variable_name,
"<br>Group:", variable_level))) +
geom_point(aes(x = !!sym(cohorts[1]),
y = !!sym(cohorts[2]))) +
geom_abline(slope = 1, intercept = 0,
color = "red", linetype = "dashed") +
theme_bw()

ggplotly(plot)

}
Loading