|
| 1 | +#' @export |
| 2 | +ui_brush_filter <- function(id) { |
| 3 | + ns <- NS(id) |
| 4 | + div( |
| 5 | + tags$h1(id = ns("title"), tags$strong("Selected points:"), class = "text-center font-150p"), |
| 6 | + teal.widgets::get_dt_rows(ns("data_table"), ns("data_table_rows")), |
| 7 | + div( |
| 8 | + actionButton(ns("apply_brush_filter"), "Apply filter"), |
| 9 | + actionButton(ns("remove_brush_filter"), "Remove applied filter") |
| 10 | + ), |
| 11 | + DT::dataTableOutput(ns("data_table"), width = "100%") |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +#' @export |
| 16 | +srv_brush_filter <- function(id, brush, data, filter_panel_api, selectors, table_dec) { |
| 17 | + moduleServer(id, function(input, output, session) { |
| 18 | + selector_list <- isolate(selectors()) |
| 19 | + |
| 20 | + observeEvent(brush(), ignoreNULL = FALSE, { |
| 21 | + if (is.null(brush())) { |
| 22 | + shinyjs::hide("title") |
| 23 | + shinyjs::hide("data_table") |
| 24 | + } else { |
| 25 | + shinyjs::show("title") |
| 26 | + shinyjs::show("data_table") |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | + states_list <- reactive({ |
| 31 | + as.list(get_filter_state(filter_panel_api)) |
| 32 | + }) |
| 33 | + |
| 34 | + observeEvent(states_list(), { |
| 35 | + brushed_states <- Filter( |
| 36 | + function(state) state$id == "brush_filter", |
| 37 | + states_list() |
| 38 | + ) |
| 39 | + if (length(brushed_states)) { |
| 40 | + shinyjs::show("remove_brush_filter") |
| 41 | + } else { |
| 42 | + shinyjs::hide("remove_brush_filter") |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + observeEvent(input$remove_brush_filter, { |
| 47 | + remove_filter_state( |
| 48 | + filter_panel_api, |
| 49 | + teal_slices( |
| 50 | + teal_slice( |
| 51 | + dataname = "ADSL", |
| 52 | + varname = "USUBJID", |
| 53 | + id = "brush_filter" |
| 54 | + ) |
| 55 | + ) |
| 56 | + ) |
| 57 | + }) |
| 58 | + |
| 59 | + brushed_table <- reactive({ |
| 60 | + plot_brush <- brush() |
| 61 | + if (is.null(plot_brush)) { |
| 62 | + return(NULL) |
| 63 | + } |
| 64 | + dataset <- isolate(teal.code::dev_suppress(data()[["ANL"]])) |
| 65 | + teal.widgets::clean_brushedPoints(dataset, plot_brush) |
| 66 | + }) |
| 67 | + |
| 68 | + observeEvent(input$apply_brush_filter, { |
| 69 | + if (is.null(input$data_table_rows_selected)) { |
| 70 | + return(NULL) |
| 71 | + } |
| 72 | + |
| 73 | + isolate({ |
| 74 | + foo1(brush, selector_list) |
| 75 | + }) |
| 76 | + |
| 77 | + brushed_df <- brushed_table()[input$data_table_rows_selected, ] |
| 78 | + # todo: when added another time then it is duplicated |
| 79 | + slice <- teal_slices(teal_slice( |
| 80 | + dataname = "ADSL", |
| 81 | + varname = "USUBJID", |
| 82 | + selected = unique(brushed_df$USUBJID), # todo: this needs to be parametrised or based on join_keys |
| 83 | + id = "brush_filter" |
| 84 | + )) |
| 85 | + set_filter_state(filter_panel_api, slice) |
| 86 | + }) |
| 87 | + |
| 88 | + output$data_table <- DT::renderDataTable(server = TRUE, { |
| 89 | + brushed_df <- brushed_table() |
| 90 | + if (is.null(brushed_df)) { |
| 91 | + return(NULL) |
| 92 | + } |
| 93 | + numeric_cols <- names(brushed_df)[ |
| 94 | + vapply(brushed_df, function(x) is.numeric(x) && !is.integer(x), FUN.VALUE = logical(1)) |
| 95 | + ] |
| 96 | + if (length(numeric_cols) > 0) { |
| 97 | + DT::formatRound( |
| 98 | + DT::datatable(brushed_df, |
| 99 | + rownames = FALSE, |
| 100 | + options = list(scrollX = TRUE, pageLength = input$data_table_rows) |
| 101 | + ), |
| 102 | + numeric_cols, |
| 103 | + table_dec |
| 104 | + ) |
| 105 | + } else { |
| 106 | + DT::datatable(brushed_df, rownames = FALSE, options = list(scrollX = TRUE, pageLength = input$data_table_rows)) |
| 107 | + } |
| 108 | + }) |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +#' get axis dataname, varname and ranges |
| 113 | +foo1 <- function(brush, selector_list) { |
| 114 | + lapply(names(brush()$mapping), function(selector) { |
| 115 | + list( |
| 116 | + dataname = selector_list[[selector]]()$dataname, |
| 117 | + varname = brush()$mapping[[selector]], |
| 118 | + values = unlist(brush()[paste0(selector, c("min", "max"))]) |
| 119 | + ) |
| 120 | + }) |
| 121 | +} |
0 commit comments