Skip to content

Commit 578e847

Browse files
authored
Trigger the reactivity on input$vals when input$col is changed (#208)
Part of the PR #207 Triggering the reactivity using `observeEvent` Example to test: ```r pkgload::load_all("../teal.modules.general") pkgload::load_all("../teal.transform") pkgload::load_all("../teal") pkgload::load_all("../teal.widgets") data <- teal_data() data <- within(data, { require(nestcolor) ADSL <- rADSL ADSL$SEX2 <- rADSL$SEX }) datanames(data) <- "ADSL" join_keys(data) <- default_cdisc_join_keys[datanames(data)] app <- init( data = data, modules = modules( tm_g_association( ref = data_extract_spec( dataname = "ADSL", filter = filter_spec(vars = choices_selected(variable_choices("ADSL"), "SEX")), 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") ) ) ) ) shinyApp(app$ui, app$server) ```
1 parent 741b615 commit 578e847

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

R/data_extract_filter_module.R

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ data_extract_filter_srv <- function(id, datasets, filter) {
6161
)
6262
})
6363

64-
output$html_vals_container <- renderUI({
64+
vals_options <- reactive({
6565
req(input$col)
66-
6766
choices <- value_choices(
6867
data = datasets[[filter$dataname]](),
6968
var_choices = input$col,
@@ -77,16 +76,38 @@ data_extract_filter_srv <- function(id, datasets, filter) {
7776
} else {
7877
choices[1L]
7978
}
79+
selected <- restoreInput(ns("vals"), selected)
80+
list(choices = choices, selected = selected)
81+
})
8082

83+
output$html_vals_container <- renderUI({
8184
teal.widgets::optionalSelectInput(
8285
inputId = ns("vals"),
8386
label = filter$label,
84-
choices = choices,
85-
selected = selected,
87+
choices = vals_options()$choices,
88+
selected = vals_options()$selected,
8689
multiple = filter$multiple,
8790
fixed = filter$fixed
8891
)
8992
})
93+
94+
# Since we want the input$vals to depend on input$col for downstream calculations,
95+
# we trigger reactivity by reassigning them. Otherwise, when input$col is changed without
96+
# a change in input$val, the downstream computations will not be triggered.
97+
observeEvent(input$col, {
98+
teal.widgets::updateOptionalSelectInput(
99+
session = session,
100+
inputId = "vals",
101+
choices = "",
102+
selected = ""
103+
)
104+
teal.widgets::updateOptionalSelectInput(
105+
session = session,
106+
inputId = "vals",
107+
choices = vals_options()$choices,
108+
selected = vals_options()$selected
109+
)
110+
})
90111
}
91112
)
92113
}

0 commit comments

Comments
 (0)