Skip to content

Commit d348719

Browse files
committed
brush_filter to the module
1 parent 12f095c commit d348719

File tree

2 files changed

+110
-74
lines changed

2 files changed

+110
-74
lines changed

R/module_brush_filter.R

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
ui_brush_filter <- function(id) {
2+
ns <- NS(id)
3+
div(
4+
uiOutput(ns("brush_filter")),
5+
DT::dataTableOutput(ns("data_table"), width = "100%")
6+
)
7+
}
8+
9+
srv_brush_filter <- function(id, brush, data, filter_panel_api, selectors, table_dec) {
10+
moduleServer(id, function(input, output, session) {
11+
selector_list <- isolate(selectors())
12+
13+
output$brush_filter <- renderUI({
14+
states <- get_filter_state(filter_panel_api)
15+
brushed_states <- Filter(
16+
function(state) state$id == "brush_filter",
17+
states
18+
)
19+
if (!is.null(brush())) {
20+
actionButton(session$ns("apply_brush_filter"), "Apply filter")
21+
} else if (length(brushed_states)) {
22+
actionButton(session$ns("remove_brush_filter"), "Remove applied filter")
23+
}
24+
})
25+
26+
observeEvent(input$remove_brush_filter, {
27+
remove_filter_state(
28+
filter_panel_api,
29+
teal_slices(
30+
teal_slice(
31+
dataname = "ADSL",
32+
varname = "USUBJID",
33+
id = "brush_filter"
34+
)
35+
)
36+
)
37+
})
38+
39+
observeEvent(input$apply_brush_filter, {
40+
plot_brush <- brush()
41+
merged_data <- isolate(teal.code::dev_suppress(data()[["ANL"]]))
42+
filter_call <- str2lang(sprintf(
43+
"merged_data <- dplyr::filter(merged_data, %1$s >= %2$s & %1$s <= %3$s & %4$s >= %5$s & %4$s <= %6$s)",
44+
plot_brush$mapping$x, plot_brush$xmin, plot_brush$xmax,
45+
plot_brush$mapping$y, plot_brush$ymin, plot_brush$ymax
46+
))
47+
eval(filter_call)
48+
49+
slice <- teal_slices(teal_slice(
50+
dataname = "ADSL",
51+
varname = "USUBJID",
52+
selected = merged_data$USUBJID,
53+
id = "brush_filter"
54+
))
55+
set_filter_state(filter_panel_api, slice)
56+
})
57+
58+
output$data_table <- DT::renderDataTable({
59+
plot_brush <- brush()
60+
if (is.null(plot_brush)) {
61+
return(NULL)
62+
}
63+
64+
isolate({
65+
foo1(brush, selector_list)
66+
})
67+
68+
dataset <- isolate(teal.code::dev_suppress(data()[["ANL"]]))
69+
brushed_df <- teal.widgets::clean_brushedPoints(dataset, plot_brush)
70+
numeric_cols <- names(brushed_df)[
71+
vapply(brushed_df, function(x) is.numeric(x) && !is.integer(x), FUN.VALUE = logical(1))
72+
]
73+
74+
if (length(numeric_cols) > 0) {
75+
DT::formatRound(
76+
DT::datatable(brushed_df,
77+
rownames = FALSE,
78+
options = list(scrollX = TRUE, pageLength = input$data_table_rows)
79+
),
80+
numeric_cols,
81+
table_dec
82+
)
83+
} else {
84+
DT::datatable(brushed_df, rownames = FALSE, options = list(scrollX = TRUE, pageLength = input$data_table_rows))
85+
}
86+
})
87+
})
88+
}
89+
90+
#' get axis dataname, varname and ranges
91+
foo1 <- function(brush, selector_list) {
92+
lapply(names(brush()$mapping), function(selector) {
93+
list(
94+
dataname = selector_list[[selector]]()$dataname,
95+
varname = brush()$mapping[[selector]],
96+
values = unlist(brush()[paste0(selector, c("min", "max"))])
97+
)
98+
})
99+
}

R/tm_g_scatterplot.R

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ ui_g_scatterplot <- function(id, ...) {
343343
teal.widgets::plot_with_settings_ui(id = ns("scatter_plot")),
344344
tags$h1(tags$strong("Selected points:"), class = "text-center font-150p"),
345345
teal.widgets::get_dt_rows(ns("data_table"), ns("data_table_rows")),
346-
uiOutput(ns("brush_filter")),
347-
DT::dataTableOutput(ns("data_table"), width = "100%")
346+
ui_brush_filter(ns("brush_filter"))
348347
),
349348
encoding = tags$div(
350349
### Reporter
@@ -1002,78 +1001,16 @@ srv_g_scatterplot <- function(id,
10021001
click = TRUE
10031002
)
10041003

1005-
output$brush_filter <- renderUI({
1006-
states <- get_filter_state(filter_panel_api)
1007-
brushed_states <- Filter(
1008-
function(state) state$id == "brush_filter",
1009-
states
1010-
)
1011-
if (!is.null(pws$brush())) {
1012-
actionButton(session$ns("apply_brush_filter"), "Apply filter")
1013-
} else if (length(brushed_states)) {
1014-
actionButton(session$ns("remove_brush_filter"), "Remove applied filter")
1015-
}
1016-
})
1017-
1018-
observeEvent(input$remove_brush_filter, {
1019-
remove_filter_state(
1020-
filter_panel_api,
1021-
teal_slices(
1022-
teal_slice(
1023-
dataname = "ADSL",
1024-
varname = "USUBJID",
1025-
id = "brush_filter"
1026-
)
1027-
)
1028-
)
1029-
})
1030-
1031-
observeEvent(input$apply_brush_filter, {
1032-
plot_brush <- pws$brush()
1033-
merged_data <- isolate(teal.code::dev_suppress(output_q()[["ANL"]]))
1034-
filter_call <- str2lang(sprintf(
1035-
"merged_data <- dplyr::filter(merged_data, %1$s >= %2$s & %1$s <= %3$s & %4$s >= %5$s & %4$s <= %6$s)",
1036-
plot_brush$mapping$x, plot_brush$xmin, plot_brush$xmax,
1037-
plot_brush$mapping$y, plot_brush$ymin, plot_brush$ymax
1038-
))
1039-
eval(filter_call)
1040-
1041-
slice <- teal_slices(teal_slice(
1042-
dataname = "ADSL",
1043-
varname = "USUBJID",
1044-
selected = merged_data$USUBJID,
1045-
id = "brush_filter"
1046-
))
1047-
set_filter_state(filter_panel_api, slice)
1048-
})
1049-
1050-
output$data_table <- DT::renderDataTable({
1051-
plot_brush <- pws$brush()
1052-
1053-
if (!is.null(plot_brush)) {
1054-
validate(need(!input$add_density, "Brushing feature is currently not supported when plot has marginal density"))
1055-
}
1056-
1057-
merged_data <- isolate(teal.code::dev_suppress(output_q()[["ANL"]]))
1058-
brushed_df <- teal.widgets::clean_brushedPoints(merged_data, plot_brush)
1059-
numeric_cols <- names(brushed_df)[
1060-
vapply(brushed_df, function(x) is.numeric(x) && !is.integer(x), FUN.VALUE = logical(1))
1061-
]
1062-
1063-
if (length(numeric_cols) > 0) {
1064-
DT::formatRound(
1065-
DT::datatable(brushed_df,
1066-
rownames = FALSE,
1067-
options = list(scrollX = TRUE, pageLength = input$data_table_rows)
1068-
),
1069-
numeric_cols,
1070-
table_dec
1071-
)
1072-
} else {
1073-
DT::datatable(brushed_df, rownames = FALSE, options = list(scrollX = TRUE, pageLength = input$data_table_rows))
1074-
}
1075-
})
1076-
1004+
# todo:
1005+
# validate(need(!input$add_density, "Brushing feature is currently not supported when plot has marginal density"))
1006+
srv_brush_filter(
1007+
"brush_filter",
1008+
brush = pws$brush,
1009+
data = output_q,
1010+
filter_panel_api = filter_panel_api,
1011+
selectors = selector_list,
1012+
table_dec = table_dec
1013+
)
10771014

10781015
teal.widgets::verbatim_popup_srv(
10791016
id = "rcode",

0 commit comments

Comments
 (0)