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" ) })