From f04fee548822b1539fc4e3f585adecccbe9bd272 Mon Sep 17 00:00:00 2001 From: jtlandis Date: Tue, 27 Feb 2024 12:14:02 -0500 Subject: [PATCH] On the fly, enforce vanilla positional scales to return on empty vectors from `scale$map()`. Fixes resurgence of #33 as seen in #58 --- R/scales-sides-.R | 21 ++++++++++++++++----- R/side-layout-.r | 6 +++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/R/scales-sides-.R b/R/scales-sides-.R index 1786e6c..ed050ec 100644 --- a/R/scales-sides-.R +++ b/R/scales-sides-.R @@ -76,14 +76,25 @@ NULL # fun # } + +mod_scale_map_method <- function(scale) { + ggproto( + NULL, + scale, + map = mod_ggproto_fun(scale$map) |> + mod_fun_at(quote(if (length(x)==0) return(x)), 1) + ) +} + new_side_pos_scale <- function(scale, side) { side <- match.arg(side, choices = c("x","y")) # other <- switch(side, x = "y", y =) - ggproto( - "ggside_scale", - scale, - aesthetics = sprintf("%sside%s", side, scale$aesthetics), - map = mod_ggproto_fun(scale$map) |> mod_fun_at(quote(if (length(x)==0) return(x)), 1) + mod_scale_map_method( + ggproto( + "ggside_scale", + scale, + aesthetics = sprintf("%sside%s", side, scale$aesthetics) + ) ) } diff --git a/R/side-layout-.r b/R/side-layout-.r index b880fd5..d9acf3f 100644 --- a/R/side-layout-.r +++ b/R/side-layout-.r @@ -68,8 +68,8 @@ new_ggside_layout <- function(layout) { find_ggside_scales = function(self, data) { params <- self$facet_params layout <- self$layout - x_scale <- self$panel_scales_x - y_scale <- self$panel_scales_y + x_scale <- lapply(self$panel_scales_x, mod_scale_map_method) + y_scale <- lapply(self$panel_scales_y, mod_scale_map_method) if ("y" %in% params$ggside$sides_used && is.null(params$ggside$ysidex)) { @@ -97,7 +97,7 @@ new_ggside_layout <- function(layout) { x_scale[side_indx] <- lapply(side_indx, function(i) params$ggside$ysidex$clone()) self$panel_scales_x <- x_scale } - y_scale <- self$panel_scales_y + if (!is.null(y_scale) && !is.null(params$ggside$xsidey) && !any(vapply(y_scale, function(scale) "xsidey" %in% scale$aesthetics, logical(1)))){ side_indx <- layout[layout$PANEL_TYPE=="x",]$SCALE_Y