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

233 delayed value choices #237

Merged

Conversation

chlebowa
Copy link
Contributor

@chlebowa chlebowa commented Jan 10, 2025

Closes #233

Opened value_choices(var_choices) to accept delayed_data.
Added a step in resolve.delayed_value_choices to resolve var_choices.
Added delayed_data class to delayed_choices functions.
Modified delayed_choices functions to return delayed_choices as is.
Modified delayed_choices functions to return arguments where x$subset is NULL as is.

Some minor adjustments.

@chlebowa chlebowa force-pushed the 233_delayed_value_choices@main branch from ed43202 to c57d565 Compare January 10, 2025 17:48
@chlebowa chlebowa force-pushed the 233_delayed_value_choices@main branch from c57d565 to 8aa5144 Compare January 10, 2025 18:18
@gogonzo gogonzo self-assigned this Jan 13, 2025
@gogonzo gogonzo added the core label Jan 14, 2025
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.

This fixes the problem. I've checked with an example app. I need more time to analyse the workflow and see if there is anything else needed.

example app
options(
  shiny.bookmarkStore = "server",
  teal.log_level = logger::DEBUG
)

tm_extract <- function(label = "PR merge",
                       info = NULL,
                       dataname = NULL,
                       data_extract,
                       pre_output = NULL,
                       post_output = NULL) {
  args <- as.list(environment())
  args$data_extract_call <- styler::style_text(
    strsplit(
      # Line break after every brace
      gsub(
        "\\((d|s|f)", "(\n\\1",
        # Line break after each function input value definition
        gsub(
          "\\,\\s([^\\=]+\\s\\=\\s)", ",\n\\1",
          paste(capture.output(match.call()$data_extract), collapse = " ")
        )
      ),
      split = "\n"
    )[[1]]
  ) |> paste(collapse = "\n")

  mod <- module(
    label = label,
    server = srv_extract,
    ui = ui_extract,
    ui_args = args,
    server_args = list(dataname = dataname, data_extract = data_extract, data_extract_call = args$data_extract_call),
    datanames = "all"
  )
  attr(mod, "teal_bookmarkable") <- TRUE
  mod
}

ui_extract <- function(id, ...) {
  arguments <- list(...)
  ns <- NS(id)
  teal.widgets::standard_layout(
    output = teal.widgets::white_small_well(
      h4("data_extract_spec"),
      verbatimTextOutput(ns("data_extract_spec")),
      h4("data_extract output"),
      verbatimTextOutput(ns("data_extract_out")),
      h4("data merge expr"),
      verbatimTextOutput(ns("merge_expr"))
    ),
    encoding = div(
      lapply(
        names(arguments$data_extract),
        function(i) {
          data_extract_ui(
            id = ns(i),
            label = paste0("Selector ", i),
            data_extract_spec = list(arguments$data_extract[[i]])
          )
        }
      )
    )
  )
}

srv_extract <- function(input, output, session, data, dataname, data_extract, data_extract_call) {
  selector_list <- teal.transform::data_extract_multiple_srv(data_extract, datasets = data)
  merged_expr <- teal.transform::merge_expression_srv(selector_list = selector_list, data = data)
  output$data_extract_spec <- renderText(data_extract_call)
  output$data_extract_out <- renderText({
    yaml::as.yaml(
      lapply(
        selector_list(),
        function(x) x()[names(x()) != "iv"]
      )
    )
  })
  output$merge_expr <- renderText(paste(merged_expr()$expr, collapse = "\n"))
}

library(teal)
pkgload::load_all("temp/teal.transform")

data <- teal_data_module(
  ui = function(id) NULL,
  server = function(id) {
    reactive({
      data <- teal_data() |> within({
        library(scda)
        ADSL <- synthetic_cdisc_data("latest")$adsl
        ADLB <- synthetic_cdisc_data("latest")$adlb
        ADTTE <- synthetic_cdisc_data("latest")$adtte
        ADRS <- synthetic_cdisc_data("latest")$adrs
        ADLB <- dplyr::mutate(ADLB, AVAL2 = 2 * AVAL)
        ADTTE <- dplyr::mutate(ADTTE, AVAL2 = 2 * AVAL)
      })
      datanames(data) <- c("ADSL", "ADLB", "ADTTE", "ADRS")
      join_keys(data) <- default_cdisc_join_keys[c("ADSL", "ADLB", "ADTTE", "ADRS")]

      data
    })
  }
)

app <- init(
  data = data,
  modules = modules(
    # single wide ----
    modules(
      label = "Single wide dataset",
      tm_extract(
        label = "dynamic input",
        data_extract = list(
          a = data_extract_spec(
            dataname = "ADSL",
            select = select_spec(
              choices = variable_choices("ADSL"),
              selected = "AGE"
            ),
            filter = filter_spec(
              vars = choices_selected(
                choices = variable_choices("ADSL", subset = function(data) {
                  names(Filter(function(x) is.factor(x) && length(levels(x)) > 1, data))
                }),
                selected = first_choice()
              ),
              choices = value_choices(
                "ADSL",
                var_choices = variable_choices("ADSL", subset = function(data) {
                  names(Filter(function(x) is.factor(x) && length(levels(x)) > 1, data))[1]
                })
              )
            )
          )
        )
      )
    )
  )
)

runApp(app)

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.

Good job. Thanks for a fix! 👍

@gogonzo gogonzo merged commit 7fd64c5 into insightsengineering:main Jan 16, 2025
28 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 16, 2025
@chlebowa chlebowa deleted the 233_delayed_value_choices@main branch January 16, 2025 12:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Question]: Does value_choices support delayed_data?
2 participants