Skip to content

Conversation

chlebowa
Copy link
Contributor

@chlebowa chlebowa commented Mar 15, 2024

Companion to insightsengineering/teal#1011
Replaces uses of update*Input with server-side input creation with renderUI.

Identified encoding elements where logic precludes bookmarking:

  • tm_g_distribution: dist_param1 and dist_param1: defaults determined dynamically in response to variable, distribution type and "Reset" button press; solved by modifying logic so that the defaults are dead before button is first pressed; added update on button label
  • tm_a_regression: label_var (outlier label) - default determined each time checkbox is TRUE, which is the case when bookmark is restored; can be partially solved by introducing an always firing version of shiny::restoreInput, which works only once; this solution would disable logic as default would no longer be recomputed
  • tm_a_pca: "plot specific settings" take reactive dependency on the qenv and cannot be bookmarked.

@chlebowa chlebowa marked this pull request as ready for review March 15, 2024 15:08
Copy link
Contributor

github-actions bot commented Mar 15, 2024

badge

Code Coverage Summary

Filename                      Stmts    Miss  Cover    Missing
--------------------------  -------  ------  -------  ------------------------------------
R/tm_a_pca.R                    833     833  0.00%    108-1074
R/tm_a_regression.R             779     779  0.00%    153-1037
R/tm_data_table.R               184     184  0.00%    93-330
R/tm_file_viewer.R              172     172  0.00%    44-250
R/tm_front_page.R               132     121  8.33%    70-226
R/tm_g_association.R            336     336  0.00%    135-543
R/tm_g_bivariate.R              674     412  38.87%   308-775, 816, 927, 944, 962, 973-995
R/tm_g_distribution.R          1055    1055  0.00%    122-1315
R/tm_g_response.R               352     352  0.00%    154-579
R/tm_g_scatterplot.R            728     728  0.00%    230-1059
R/tm_g_scatterplotmatrix.R      284     265  6.69%    165-478, 539, 553
R/tm_missing_data.R            1063    1063  0.00%    92-1311
R/tm_outliers.R                 991     991  0.00%    134-1269
R/tm_t_crosstable.R             257     257  0.00%    141-446
R/tm_variable_browser.R         827     822  0.60%    79-1067, 1105-1289
R/utils.R                        99      96  3.03%    82-267
R/zzz.R                           2       2  0.00%    2-3
TOTAL                          8768    8468  3.42%

Diff against main

Filename                      Stmts    Miss  Cover
--------------------------  -------  ------  --------
R/tm_a_pca.R                     +6      +6  +100.00%
R/tm_a_regression.R              +3      +3  +100.00%
R/tm_data_table.R                +5      +5  +100.00%
R/tm_file_viewer.R               +2      +2  +100.00%
R/tm_front_page.R                +3      +3  -0.19%
R/tm_g_association.R             +2      +2  +100.00%
R/tm_g_bivariate.R               +3      +1  +0.12%
R/tm_g_distribution.R           +13     +13  +100.00%
R/tm_g_response.R                +2      +2  +100.00%
R/tm_g_scatterplot.R             +2      +2  +100.00%
R/tm_g_scatterplotmatrix.R       +2      +2  -0.05%
R/tm_missing_data.R              +5      +5  +100.00%
R/tm_outliers.R                  +3      +3  +100.00%
R/tm_t_crosstable.R              +2      +2  +100.00%
R/tm_variable_browser.R          +2      +2  -0.00%
TOTAL                           +55     +53  +0.00%

Results for commit: 9623d52

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

Copy link
Contributor

github-actions bot commented Mar 15, 2024

Unit Tests Summary

 1 files   6 suites   0s ⏱️
29 tests 29 ✅ 0 💤 0 ❌
67 runs  67 ✅ 0 💤 0 ❌

Results for commit 9623d52.

♻️ This comment has been updated with latest results.

@gogonzo
Copy link
Contributor

gogonzo commented Mar 26, 2024

testing app function

testing shiny app code
options(teal.log_level = "TRACE", teal.show_js_log = TRUE)
library(shiny)
library(teal.widgets)
library(teal.transform)
pkgload::load_all("teal.modules.general")

data <- within(teal_data(), {
  require(nestcolor)
  IRIS <- iris
  MTCARS <- mtcars
  USArrests <- USArrests
  ADSL <- rADSL
  ADTTE <- rADTTE
  ADRS <- rADRS
})
datanames(data) <- c("IRIS", "MTCARS", "USArrests", "ADSL", "ADTTE", "ADRS")
join_keys(data) <- default_cdisc_join_keys[datanames(data)]

shiny_app <- function(module) {
  ui <- function(request) {
    fluidPage(
      bookmarkButton(),
      do.call(module$ui, c(list(id = "teal_module"), module$ui_args))
    )
  }

  server <- function(input, output, session) {
    data_list <-
      args <- c(
        list(id = "teal_module"),
        if ("data" %in% names(formals(module$server))) {
          list(data = reactive(data))
        },
        module$server_args
      )
    do.call(module$server, args)
  }

  shinyApp(ui, server, enableBookmarking = "server")
}

tm_a_pca ❌

app code
shiny_app(
  tm_a_pca(
    "PCA",
    dat = data_extract_spec(
      dataname = "USArrests",
      select = select_spec(
        choices = variable_choices(
          data = "USArrests", c("Murder", "Assault", "UrbanPop", "Rape")
        ),
        selected = c("Murder", "Assault"),
        multiple = TRUE
      ),
      filter = NULL
    ),
    ggplot2_args = ggplot2_args(
      labs = list(subtitle = "Plot generated by PCA Module")
    )
  )
)

tm_a_regression ❌

app code
shiny_app(
  tm_a_regression(
    label = "Regression",
    response = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = "BMRKR1",
        selected = "BMRKR1",
        multiple = FALSE,
        fixed = TRUE
      )
    ),
    regressor = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variables:",
        choices = variable_choices(data[["ADSL"]], c("AGE", "SEX", "RACE")),
        selected = "AGE",
        multiple = TRUE,
        fixed = FALSE
      )
    ),
    ggplot2_args = ggplot2_args(
      labs = list(subtitle = "Plot generated by Regression Module")
    )
  )
)

tm_data_table ✅

app code
shiny_app(teal.modules.general::tm_data_table())

tm_file_viewer ❌

app code
shiny_app(
  tm_file_viewer(
    input_path = list(
      folder = system.file("sample_files", package = "teal.modules.general"),
      png = system.file("sample_files/sample_file.png", package = "teal.modules.general"),
      txt = system.file("sample_files/sample_file.txt", package = "teal.modules.general"),
      url = "https://fda.gov/files/drugs/published/Portable-Document-Format-Specifications.pdf"
    )
  )
)

tm_g_association ✅

app code
shiny_app(
  tm_g_association(
    ref = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = variable_choices(
          data[["ADSL"]],
          c("SEX", "RACE", "COUNTRY", "ARM", "STRATA1", "STRATA2", "ITTFL", "BMRKR2")
        ),
        selected = "RACE",
        fixed = FALSE
      )
    ),
    vars = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variables:",
        choices = variable_choices(
          data[["ADSL"]],
          c("SEX", "RACE", "COUNTRY", "ARM", "STRATA1", "STRATA2", "ITTFL", "BMRKR2")
        ),
        selected = "BMRKR2",
        multiple = TRUE,
        fixed = FALSE
      )
    ),
    ggplot2_args = ggplot2_args(
      labs = list(subtitle = "Plot generated by Association Module")
    )
  )
)

tm_g_bivariate ✅

app code
shiny_app(
  tm_g_bivariate(
    x = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = variable_choices(data[["ADSL"]]),
        selected = "AGE",
        fixed = FALSE
      )
    ),
    y = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = variable_choices(data[["ADSL"]]),
        selected = "SEX",
        multiple = FALSE,
        fixed = FALSE
      )
    ),
    row_facet = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = variable_choices(data[["ADSL"]]),
        selected = "ARM",
        fixed = FALSE
      )
    ),
    col_facet = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = variable_choices(data[["ADSL"]]),
        selected = "COUNTRY",
        fixed = FALSE
      )
    ),
    ggplot2_args = ggplot2_args(
      labs = list(subtitle = "Plot generated by Bivariate Module")
    )
  )
)

tm_g_distribution ✅

app code
vars1 <- choices_selected(
  variable_choices(data[["ADSL"]], c("ARM", "COUNTRY", "SEX")),
  selected = NULL
)
module <- tm_g_distribution(
  dist_var = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")),
      selected = "BMRKR1",
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  strata_var = data_extract_spec(
    dataname = "ADSL",
    filter = filter_spec(
      vars = vars1,
      multiple = TRUE
    )
  ),
  group_var = data_extract_spec(
    dataname = "ADSL",
    filter = filter_spec(
      vars = vars1,
      multiple = TRUE
    )
  ),
  ggplot2_args = ggplot2_args(
    labs = list(subtitle = "Plot generated by Distribution Module")
  )
)

shiny_app(module)

tm_g_response ✅

app code
module <- tm_g_response(
  label = "Response Plots",
  response = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(data[["ADSL"]], c("BMRKR2", "COUNTRY")),
      selected = "BMRKR2",
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  x = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(data[["ADSL"]], c("SEX", "RACE")),
      selected = "RACE",
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  ggplot2_args = ggplot2_args(
    labs = list(subtitle = "Plot generated by Response Module")
  )
)
shiny_app(module)

tm_g_scatterplot ✅

app code
module <- tm_g_scatterplot(
  label = "Scatterplot Choices",
  x = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")),
      selected = "AGE",
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  y = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")),
      selected = "BMRKR1",
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  color_by = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(
        data[["ADSL"]],
        c("AGE", "BMRKR1", "BMRKR2", "RACE", "REGION1")
      ),
      selected = NULL,
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  size_by = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")),
      selected = "AGE",
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  row_facet = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")),
      selected = NULL,
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  col_facet = data_extract_spec(
    dataname = "ADSL",
    select = select_spec(
      label = "Select variable:",
      choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")),
      selected = NULL,
      multiple = FALSE,
      fixed = FALSE
    )
  ),
  ggplot2_args = ggplot2_args(
    labs = list(subtitle = "Plot generated by Scatterplot Module")
  )
)
shiny_app(module)

tm_g_scatterplotmatrix ✅

app code
shiny_app(
  tm_g_scatterplotmatrix(
    label = "Scatterplot matrix",
    variables = list(
      data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variables:",
          choices = variable_choices(data[["ADSL"]]),
          selected = c("AGE", "RACE", "SEX"),
          multiple = TRUE,
          ordered = TRUE,
          fixed = FALSE
        )
      ),
      data_extract_spec(
        dataname = "ADRS",
        filter = filter_spec(
          label = "Select endpoints:",
          vars = c("PARAMCD", "AVISIT"),
          choices = value_choices(data[["ADRS"]], c("PARAMCD", "AVISIT"), c("PARAM", "AVISIT")),
          selected = "INVET - END OF INDUCTION",
          multiple = TRUE
        ),
        select = select_spec(
          label = "Select variables:",
          choices = variable_choices(data[["ADRS"]]),
          selected = c("AGE", "AVAL", "ADY"),
          multiple = TRUE,
          ordered = TRUE,
          fixed = FALSE
        )
      )
    )
  )
)

tm_missing_data ✅

app code
shiny_app(
  tm_missing_data()
)

tm_outliers ✅

app code
fact_vars_adsl <- names(Filter(isTRUE, sapply(data[["ADSL"]], is.factor)))
vars <- choices_selected(variable_choices(data[["ADSL"]], fact_vars_adsl))
shiny_app(
  tm_outliers(
    outlier_var = list(
      data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")),
          selected = "AGE",
          multiple = FALSE,
          fixed = FALSE
        )
      )
    ),
    categorical_var = list(
      data_extract_spec(
        dataname = "ADSL",
        filter = filter_spec(
          vars = vars,
          choices = value_choices(data[["ADSL"]], vars$selected),
          selected = value_choices(data[["ADSL"]], vars$selected),
          multiple = TRUE
        )
      )
    ),
    ggplot2_args = list(
      ggplot2_args(
        labs = list(subtitle = "Plot generated by Outliers Module")
      )
    )
  )
)

tm_t_crosstable ✅

app code
shiny_app(
  tm_t_crosstable(
    label = "Cross Table",
    x = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = variable_choices(data[["ADSL"]], subset = function(data) {
          idx <- !vapply(data, inherits, logical(1), c("Date", "POSIXct", "POSIXlt"))
          return(names(data)[idx])
        }),
        selected = "COUNTRY",
        multiple = TRUE,
        ordered = TRUE,
        fixed = FALSE
      )
    ),
    y = data_extract_spec(
      dataname = "ADSL",
      select = select_spec(
        label = "Select variable:",
        choices = variable_choices(data[["ADSL"]], subset = function(data) {
          idx <- vapply(data, is.factor, logical(1))
          return(names(data)[idx])
        }),
        selected = "SEX",
        multiple = FALSE,
        fixed = FALSE
      )
    ),
    basic_table_args = basic_table_args(
      subtitles = "Table generated by Crosstable Module"
    )
  )
)

tm_t_variable_browser ✅

app code
shiny_app(tm_t_variable_browser())

Copy link
Contributor

@gogonzo gogonzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@chlebowa chlebowa merged commit 6f19601 into main Mar 28, 2024
@chlebowa chlebowa deleted the 898_save_app_state3@main branch March 28, 2024 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants