Skip to content

Commit

Permalink
Clean things up for readability. (#287)
Browse files Browse the repository at this point in the history
* Clean things up for readability.

* Add gt to Imports to elevate from gsm Suggests.
  • Loading branch information
jonthegeek authored Oct 16, 2024
1 parent af109b8 commit 73f8b0f
Show file tree
Hide file tree
Showing 25 changed files with 380 additions and 158 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
DT,
glue,
gsm (>= 2.1.0),
gt,
htmltools,
htmlwidgets,
jsonlite,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(sample_FetchParticipantData)
import(shiny)
importFrom(cli,cli_alert)
importFrom(cli,cli_alert_info)
importFrom(gt,gt)
importFrom(kableExtra,kbl)
importFrom(magrittr,"%>%")
importFrom(rlang,.data)
2 changes: 2 additions & 0 deletions R/aaa-shared.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#' @param dfGroups `data.frame` Group-level metadata dictionary.
#' @param dfMetrics `data.frame` Metric-specific metadata for use in charts and
#' reporting.
#' @param dfParticipantGroups `data.frame` Unique `SubjectID` and `GroupID`
#' combos from `dfAnalyticsInput`.
#' @param dfResults `data.frame` A stacked summary of analysis pipeline output.
#' This will be filtered to cases where `GroupLevel == "Site"`.
#' @param envCall `environment` The environment from which this function was
Expand Down
1 change: 1 addition & 0 deletions R/gsm.app-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @import shiny
#' @importFrom cli cli_alert
#' @importFrom cli cli_alert_info
#' @importFrom gt gt
#' @importFrom kableExtra kbl
#' @importFrom rlang .data
## usethis namespace: end
Expand Down
137 changes: 50 additions & 87 deletions R/gsmApp_Server.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,57 @@
#' @returns The main server function for use in a shiny app.
#' @keywords internal
gsmApp_Server <- function(
dfResults,
dfAnalyticsInput,
dfBounds,
dfGroups,
dfMetrics,
dfBounds,
dfAnalyticsInput,
dfResults,
fnFetchParticipantData,
fnServer = NULL
) {
# Force evaluation of everything before factory is constructed to avoid
# strange effects from lazy evaluation. See
# https://adv-r.hadley.nz/function-factories.html#forcing-evaluation
force(dfResults)
force(dfAnalyticsInput)
force(dfBounds)
force(dfGroups)
force(dfMetrics)
force(dfBounds)
force(dfAnalyticsInput)
force(dfResults)
force(fnFetchParticipantData)
force(fnServer)
function(input, output, session) {
if (!is.null(fnServer)) {
fnServer(input, output, session)
}

# Inputs ----
## Initialize ----
dfAnalyticsInput_Unique <- dfAnalyticsInput %>%
dplyr::distinct(.data$SubjectID, .data$GroupID) %>%
dplyr::arrange(.data$SubjectID)
chrParticipantIDs <- dfAnalyticsInput_Unique$SubjectID

## Reset ----
observe({
updateSelectInput(session, "metric", selected = dfMetrics$MetricID[[1]])
updateSelectInput(session, "site", selected = "None")
updateSelectizeInput(
"participant",
choices = c("None", chrParticipantIDs),
selected = "None",
server = TRUE,
session = session
)
bslib::nav_select("primary_nav_bar", "Study Overview")
}) %>%
bindEvent(input$reset)
# Reset ----
dfParticipantGroups <- make_dfParticipantGroups(dfAnalyticsInput)
srvr_Reset(dfMetrics, dfParticipantGroups, reactive(input$reset), session)

# Shared Reactives ----

## Inputs pass to modules (etc) as reactives.
rctv_InputMetric <- reactive(input$metric)
rctv_InputSite <- reactive(input$site)

## The listified dfMetrics are used by both metric sub-mods, so calculate
## them once. This can/should move inside a single metric-tab module.
rctv_lMetric_base <- reactive({
as.list(
filter_byMetricID(dfMetrics, input$metric)
)
}) %>%
bindCache(input$metric)
rctv_lMetric <- reactive({
lMetric <- rctv_lMetric_base()
if (input$site != "None") {
lMetric$selectedGroupIDs <- input$site
}
lMetric
}) %>%
shiny::bindCache(input$metric, input$site)

rctv_chrParticipantIDs <- reactive({
if (input$site == "None") {
return(c("None", dfAnalyticsInput_Unique$SubjectID))
}
c(
"None",
dfAnalyticsInput_Unique$SubjectID[
dfAnalyticsInput_Unique$GroupID == input$site
]
)
}) %>%
bindCache(input$site)
rctv_lMetric_base <- srvr_rctv_lMetric_base(
dfMetrics,
rctv_InputMetric,
session
)
rctv_lMetric <- srvr_rctv_lMetric(
dfMetrics,
rctv_lMetric_base,
rctv_InputMetric,
rctv_InputSite,
session
)
rctv_chrParticipantIDs <- srvr_rctv_chrParticipantIDs(
dfParticipantGroups,
rctv_InputSite
)

# Tab Contents ----

Expand All @@ -90,7 +65,7 @@ gsmApp_Server <- function(
dfGroups = dfGroups,
dfMetrics = dfMetrics,
dfBounds = dfBounds,
rctv_strSiteID = reactive(input$site)
rctv_strSiteID = rctv_InputSite
)
srvr_SyncSelectInput(
"site",
Expand All @@ -104,44 +79,33 @@ gsmApp_Server <- function(
)

## Metric Details ----
srvr_SyncTab(
"primary_nav_bar",
"Metric Details",
reactive(input$metric),
session
srvr_SyncTab("primary_nav_bar", "Metric Details", rctv_InputMetric, session)
rctv_strMetricDetailsGroup <- mod_MetricDetails_Server(
"metric_details",
dfResults = dfResults,
dfGroups = dfGroups,
dfBounds = dfBounds,
rctv_lMetric = rctv_lMetric,
rctv_strSiteID = rctv_InputSite,
rctv_strMetricID = rctv_InputMetric
)
## Don't render until it loads. We should be able to fix this later once
## nested-modules are implemented cleanly.
bindEvent(
{
rctv_strMetricDetailsGroup <- mod_MetricDetails_Server(
"metric_details",
dfResults = dfResults,
dfGroups = dfGroups,
dfBounds = dfBounds,
rctv_lMetric = rctv_lMetric,
rctv_strSiteID = reactive(input$site),
rctv_strMetricID = reactive(input$metric)
)
rctv_strSiteDetailsParticipant <- mod_SiteDetails_Server(
"site_details",
dfGroups = dfGroups,
dfAnalyticsInput = dfAnalyticsInput,
rctv_strSiteID = reactive(input$site),
rctv_strMetricID = reactive(input$metric),
rctv_lMetric = rctv_lMetric
)
},
input$primary_nav_bar == "Metric Details",
ignoreInit = TRUE,
once = TRUE
rctv_strSiteDetailsParticipant <- mod_SiteDetails_Server(
"site_details",
dfGroups = dfGroups,
dfAnalyticsInput = dfAnalyticsInput,
rctv_strSiteID = rctv_InputSite,
rctv_strMetricID = rctv_InputMetric,
rctv_lMetric = rctv_lMetric
)
srvr_SyncSelectInput("site", rctv_strMetricDetailsGroup, session)

# Temporary: Update Site drop-down when one of the non-module widgets
# changes its value without sending a full Shiny event.
srvr_SyncSelectInput("site", reactive(input$site), session)
srvr_SyncSelectInput("site", rctv_InputSite, session)

### Sync participant dropdown filter ----
###
### Revisit as app becomes fully modularized.
rctv_LatestParticipant <- reactiveVal("None")
observe({
req(input$participant)
Expand Down Expand Up @@ -207,7 +171,6 @@ gsmApp_Server <- function(
srvr_SyncTab(
"primary_nav_bar",
"Participant Details",
# reactive(input$participant),
rctv_LatestParticipant,
session
)
Expand Down
7 changes: 4 additions & 3 deletions R/gsmApp_UI.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#' @returns A Shiny UI object
#' @keywords internal
gsmApp_UI <- function(
dfResults,
dfMetrics,
dfAnalyticsInput,
dfGroups,
intNParticipants,
dfMetrics,
dfResults,
strTitle = "GSM Deep Dive",
tagListSidebar = NULL
) {
# Transform data for use in lower-level functions. ----
intNParticipants <- length(unique(dfAnalyticsInput$SubjectID))
lStudy <- make_lStudy(dfGroups, dfResults)
chrMetrics <- rlang::set_names(dfMetrics$MetricID, dfMetrics$Metric)
chrSites <- sort(unique(dfGroups$GroupID[dfGroups$GroupLevel == "Site"]))
Expand Down
3 changes: 0 additions & 3 deletions R/mod_SiteDetails_Server.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ mod_SiteDetails_Server <- function(

# This code will be removed in an upcoming update (when these are treated as
# proper modules).
#
# nocov start
observe({
if (rctv_strSiteID() == "None") {
## Show placeholders
Expand All @@ -77,7 +75,6 @@ mod_SiteDetails_Server <- function(
shinyjs::show("card_participants")
}
})
# nocov end

### Site Metadata
output$site_metadata_list <- renderUI({
Expand Down
34 changes: 17 additions & 17 deletions R/run_gsm_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@
#' run_sample_gsm_app()
#' @export
run_gsm_app <- function(
dfResults,
dfAnalyticsInput,
dfBounds,
dfGroups,
dfMetrics,
dfBounds,
dfAnalyticsInput,
dfResults,
fnFetchParticipantData,
strTitle = "GSM Deep Dive",
tagListSidebar = NULL,
fnServer = NULL
) {
# There's no point launching the app if the data won't work.
dfResults <- validate_dfResults(dfResults)
dfAnalyticsInput <- validate_dfAnalyticsInput(dfAnalyticsInput)
dfBounds <- validate_dfBounds(dfBounds)
dfGroups <- validate_dfGroups(dfGroups)
dfMetrics <- validate_dfMetrics(dfMetrics)
dfBounds <- validate_dfBounds(dfBounds)
dfAnalyticsInput <- validate_dfAnalyticsInput(dfAnalyticsInput)
dfResults <- validate_dfResults(dfResults)

# We currently only use site-level data in this app.
dfResults <- dfResults[dfResults$GroupLevel == "Site", ]
dfAnalyticsInput <- dfAnalyticsInput[dfAnalyticsInput$GroupLevel == "Site", ]
dfResults <- dfResults[dfResults$GroupLevel == "Site", ]

shinyApp(
ui = gsmApp_UI(
dfResults = dfResults,
dfMetrics = dfMetrics,
dfAnalyticsInput = dfAnalyticsInput,
dfGroups = dfGroups,
intNParticipants = length(unique(dfAnalyticsInput$SubjectID)),
dfMetrics = dfMetrics,
dfResults = dfResults,
strTitle = strTitle,
tagListSidebar = tagListSidebar
),
server = gsmApp_Server(
dfResults = dfResults,
dfAnalyticsInput = dfAnalyticsInput,
dfBounds = dfBounds,
dfGroups = dfGroups,
dfMetrics = dfMetrics,
dfBounds = dfBounds,
dfAnalyticsInput = dfAnalyticsInput,
dfResults = dfResults,
fnFetchParticipantData = fnFetchParticipantData,
fnServer = fnServer
)
Expand All @@ -64,12 +64,12 @@ run_gsm_app <- function(
#' @export
run_sample_gsm_app <- function() { # nocov start
run_gsm_app(
dfResults = gsm.app::sample_dfResults,
dfAnalyticsInput = gsm.app::sample_dfAnalyticsInput,
dfBounds = gsm.app::sample_dfBounds,
dfGroups = gsm.app::sample_dfGroups,
dfMetrics = gsm.app::sample_dfMetrics,
dfBounds = gsm.app::sample_dfBounds,
dfAnalyticsInput = gsm.app::sample_dfAnalyticsInput,
fnFetchParticipantData = gsm.app::sample_FetchParticipantData,
dfResults = gsm.app::sample_dfResults,
fnFetchParticipantData = sample_FetchParticipantData,
strTitle = "Sample Deep Dive App"
)
} # nocov end
Loading

0 comments on commit 73f8b0f

Please sign in to comment.