`data_extract_filter_srv()` drops `filter$sep` when recomputing choices, so multi-variable `filter_spec` loses its selection

Aperta
#297 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

  • #298 di @kaipingyang — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
r
Ambito
backend

Direzione di ricerca

Read R/data_extract_filter_module.R, focusing on data_extract_filter_srv() and get_initial_filter_values(), then trace how value_choices() uses filter$sep. Reproduce the reported case with two variables and sep = "~", and check the vals updates after changing columns. Done means recomputed choices preserve the separator and selected values remain valid; add regression coverage for both code paths.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What happens

When a filter_spec() is built on two or more variables with a non-default sep (for example sep = "~"), the filter widget works at first. After the user changes the filter column(s), the observer in data_extract_filter_srv() recomputes the value choices with value_choices() but does not pass filter$sep. The new choices are joined with the default " - ", while filter$selected is still joined with "~". The selected value no longer matches any choice.

Downstream, the extracted filter is split with filter$sep, so the mismatch has two possible outcomes:

  • the vals picker is cleared, the input becomes invalid, and the module never renders, or
  • a value like "PRIOR MED - Y" cannot be split on "~", and the resulting filter compares each variable to the whole string, so it matches no rows. The output is empty or all zeros, with no error.

Only filter_specs with two or more vars are affected, because a single-variable filter never joins values.

Reproducible example
library(teal.transform)
library(shiny)

ADSL <- data.frame(
  USUBJID = paste0("S", 1:4),
  CMCAT   = c("PRIOR MED", "PRIOR MED", "CONCOMITANT MED", "CONCOMITANT MED"),
  PRIORFL = c("Y", "N", "Y", "N")
)

fs <- filter_spec(
  vars     = c("CMCAT", "PRIORFL"),
  sep      = "~",
  choices  = value_choices(ADSL, c("CMCAT", "PRIORFL"), sep = "~"),
  selected = "PRIOR MED~Y",
  multiple = FALSE
)
fs$dataname <- "ADSL"

# record every update sent to the `vals` widget
log <- list()
orig <- teal.widgets::updateOptionalSelectInput
utils::assignInNamespace("updateOptionalSelectInput", function(session, inputId, choices = NULL, selected = NULL, ...) {
  if (inputId == "vals") log[[length(log) + 1]] <<- list(choices = as.character(choices), selected = selected)
  orig(session, inputId, choices = choices, selected = selected, ...)
}, ns = "teal.widgets")

testServer(
  teal.transform:::data_extract_filter_srv,
  args = list(id = "f", datasets = list(ADSL = reactive(ADSL)), filter = fs),
  {
    session$setInputs(col = c("CMCAT", "PRIORFL"))
    session$setInputs(col = "CMCAT")
    session$setInputs(col = c("CMCAT", "PRIORFL"))
  }
)

final <- log[[length(log)]]
final$choices
#> [1] "PRIOR MED - Y"       "PRIOR MED - N"       "CONCOMITANT MED - Y" "CONCOMITANT MED - N"
final$selected
#> [1] "PRIOR MED~Y"
final$selected %in% final$choices
#> [1] FALSE

The first update (from the initial isolate() block) correctly uses filter$choices, which are joined with "~". Every later update from the observeEvent(input$col) handler uses " - ".

Cause

R/data_extract_filter_module.R, inside observeEvent(input$col, ...) (on main as of 2026-11-30):

choices <- value_choices(
  datasets[[filter$dataname]](),
  input$col,
  `if`(isTRUE(input$col == attr(filter$choices, "var_choices")), attr(filter$choices, "var_label"), NULL)
)

sep is not passed, so value_choices() falls back to its default " - ".

get_initial_filter_values() in the same file has the same pattern (value_choices(datasets[[filter$dataname]](), as.character(filter$vars_selected))). It looks like the same bug whenever filter$choices is NULL and more than one variable is selected.

Suggested fix
             choices <- value_choices(
               datasets[[filter$dataname]](),
               input$col,
-              `if`(isTRUE(input$col == attr(filter$choices, "var_choices")), attr(filter$choices, "var_label"), NULL)
+              `if`(isTRUE(input$col == attr(filter$choices, "var_choices")), attr(filter$choices, "var_label"), NULL),
+              sep = filter$sep
             )

and the same sep = filter$sep in get_initial_filter_values().

The handler also keeps filter$selected even when it is not one of the new choices. The third recorded update above, after switching to a single column, sends selected = "PRIOR MED~Y" together with choices "PRIOR MED" and "CONCOMITANT MED". Keeping only the selected values that are in choices, and otherwise falling back to the existing default, would make this path predictable:

selected <- if (!is.null(filter$selected) && any(filter$selected %in% choices)) {
  intersect(filter$selected, choices)
} else if (filter$multiple) {
  choices
} else {
  choices[1]
}

I'm happy to open a PR with both changes and a test.

Session info
  • teal.transform 0.7.2 (CRAN) and main @ 1ef30de9
  • teal.widgets 0.6.1, shiny 1.15.0, R 4.4.3
Lingua principale
R
Stelle
2
Fork
5
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pharmaverse/teal.transform

Tutte le issue di pharmaverse/teal.transform

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.