From 14999716fe20a8b2c10c416691b9f34f4600aede Mon Sep 17 00:00:00 2001 From: go_gonzo Date: Wed, 20 Nov 2024 14:32:26 +0100 Subject: [PATCH] wip brushing --- R/plot_with_settings.R | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/R/plot_with_settings.R b/R/plot_with_settings.R index 2bf18978..67951037 100644 --- a/R/plot_with_settings.R +++ b/R/plot_with_settings.R @@ -406,8 +406,6 @@ plot_with_settings_srv <- function(id, } }) - - plotly_brush <- reactive({ req(plot_suppress(plot_r())) # layer_data(plot_r(), 3) @@ -416,13 +414,33 @@ plot_with_settings_srv <- function(id, if (is.null(bbox)) { return(NULL) } + + # aes() can be specified in ggplot() call or in geoms (layers) + facets_mapping <- if (!is.null(plot_r()$facet)) { + facets_quo <- unlist(plot_r()$facet$params[c("facets", "rows", "cols")], recursive = FALSE, use.names = FALSE) + unique(unlist(lapply(facets_quo, rlang::as_label))) + # todo: missing levels where brushing occurred - we don't know which facet has been brushed + # although we know which datapoints x and y has been selected on the plot + # we can match them with original data outside of this module... see returned $x and $y + } + + layers_mapping <- lapply(c(x = "x", y = "y"), function(axis) { + cols <- unique( + c( + rlang::as_label(plot_r()$mapping[[axis]]), + sapply(plot_r()$layers, function(layer) rlang::as_label(layer$mapping[[axis]])) + ) + ) + setdiff(cols, "NULL") + }) + list( - mapping = list( - x = rlang::as_label(plot_r()$mapping$x), - y = rlang::as_label(plot_r()$mapping$y) - ), + mapping = layers_mapping, xmin = min(bbox$x), xmax = max(bbox$x), ymin = min(bbox$y), ymax = max(bbox$y), + facet_mapping = facets_mapping, + x = bbox$x, + y = bbox$y, direction = "xy" ) })