Skip to content

Commit a9c9b06

Browse files
committed
scatterplot + data_table
1 parent 842ba1b commit a9c9b06

File tree

3 files changed

+69
-126
lines changed

3 files changed

+69
-126
lines changed

R/module_brush_filter.R

Lines changed: 0 additions & 120 deletions
This file was deleted.

R/tm_data_table.R

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ srv_page_data_table <- function(id,
187187
variables_selected,
188188
dt_args,
189189
dt_options,
190-
server_rendering) {
190+
server_rendering,
191+
filter_panel_api) {
191192
checkmate::assert_class(data, "reactive")
192193
checkmate::assert_class(isolate(data()), "teal_data")
193194
moduleServer(id, function(input, output, session) {
@@ -262,7 +263,8 @@ srv_page_data_table <- function(id,
262263
if_distinct = if_distinct,
263264
dt_args = dt_args,
264265
dt_options = dt_options,
265-
server_rendering = server_rendering
266+
server_rendering = server_rendering,
267+
filter_panel_api = filter_panel_api
266268
)
267269
}
268270
)
@@ -283,6 +285,10 @@ ui_data_table <- function(id,
283285

284286
tagList(
285287
teal.widgets::get_dt_rows(ns("data_table"), ns("dt_rows")),
288+
div(
289+
actionButton(ns("apply_brush_filter"), "Apply filter"),
290+
actionButton(ns("remove_brush_filter"), "Remove applied filter")
291+
),
286292
fluidRow(
287293
teal.widgets::optionalSelectInput(
288294
ns("variables"),
@@ -307,7 +313,8 @@ srv_data_table <- function(id,
307313
if_distinct,
308314
dt_args,
309315
dt_options,
310-
server_rendering) {
316+
server_rendering,
317+
filter_panel_api) {
311318
moduleServer(id, function(input, output, session) {
312319
iv <- shinyvalidate::InputValidator$new()
313320
iv$add_rule("variables", shinyvalidate::sv_required("Please select valid variable names"))
@@ -338,5 +345,61 @@ srv_data_table <- function(id,
338345

339346
do.call(DT::datatable, dt_args)
340347
})
348+
349+
observeEvent(input$data_table_rows_selected, ignoreNULL = FALSE, {
350+
if (is.null(input$data_table_rows_selected)) {
351+
shinyjs::hide("apply_brush_filter")
352+
} else {
353+
shinyjs::show("apply_brush_filter")
354+
}
355+
})
356+
357+
observeEvent(input$apply_brush_filter, {
358+
if (is.null(input$data_table_rows_selected)) {
359+
return(NULL)
360+
}
361+
# isolate({
362+
# foo1(brush, selector_list)
363+
# })
364+
dataset <- data()[[dataname]][input$data_table_rows_selected, ]
365+
# todo: when added another time then it is duplicated
366+
slice <- teal_slices(teal_slice(
367+
dataname = "ADSL",
368+
varname = "USUBJID",
369+
selected = unique(dataset$USUBJID), # todo: this needs to be parametrised or based on join_keys
370+
id = "brush_filter"
371+
))
372+
shinyjs::hide("apply_brush_filter")
373+
set_filter_state(filter_panel_api, slice)
374+
})
375+
376+
states_list <- reactive({
377+
as.list(get_filter_state(filter_panel_api))
378+
})
379+
380+
observeEvent(input$remove_brush_filter, {
381+
remove_filter_state(
382+
filter_panel_api,
383+
teal_slices(
384+
teal_slice(
385+
dataname = "ADSL",
386+
varname = "USUBJID",
387+
id = "brush_filter"
388+
)
389+
)
390+
)
391+
})
392+
393+
observeEvent(states_list(), {
394+
brushed_states <- Filter(
395+
function(state) state$id == "brush_filter",
396+
states_list()
397+
)
398+
if (length(brushed_states)) {
399+
shinyjs::show("remove_brush_filter")
400+
} else {
401+
shinyjs::hide("remove_brush_filter")
402+
}
403+
})
341404
})
342405
}

R/tm_g_scatterplot.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ ui_g_scatterplot <- function(id, ...) {
341341
teal.widgets::standard_layout(
342342
output = teal.widgets::white_small_well(
343343
teal.widgets::plot_with_settings_ui(id = ns("scatter_plot")),
344-
ui_brush_filter(ns("brush_filter"))
344+
teal::ui_brush_filter(ns("brush_filter"))
345345
),
346346
encoding = tags$div(
347347
### Reporter
@@ -1001,10 +1001,10 @@ srv_g_scatterplot <- function(id,
10011001

10021002
# todo:
10031003
# validate(need(!input$add_density, "Brushing feature is currently not supported when plot has marginal density"))
1004-
srv_brush_filter(
1004+
teal::srv_brush_filter(
10051005
"brush_filter",
10061006
brush = pws$brush,
1007-
data = output_q,
1007+
dataset = reactive(teal.code::dev_suppress(output_q()[["ANL"]])),
10081008
filter_panel_api = filter_panel_api,
10091009
selectors = selector_list,
10101010
table_dec = table_dec

0 commit comments

Comments
 (0)