|
21 | 21 | #' @param shared_axis_size if simplify_shared_axes is true, this determines the size of the shared axis relative to the size of a single plot |
22 | 22 | #' @param template a template plot (ggplot object) to use for the stacked plots |
23 | 23 | #' @param add a list of ggplot component calls to add to specific panel plots, either by panel variable name (named list) or index (unnamed list) |
24 | | -#' @param debug debug flag to print the stackplot tibble and gtable intermediates |
| 24 | +#' @param debug `r lifecycle::badge("experimental")` debug flag to print the stackplot tibble and gtable intermediates |
25 | 25 | #' @examples |
26 | 26 | #' |
27 | 27 | #' # 1 step stackplot (most common use) |
@@ -122,48 +122,48 @@ prepare_stackplot <- function( |
122 | 122 |
|
123 | 123 | # internal function to prepare the data for a ggstackplot |
124 | 124 | create_stackplot_tibble <- function( |
125 | | - data, x, y, remove_na = TRUE, color = NA, palette = NA, both_axes = FALSE, alternate_axes = FALSE, switch_axes = FALSE) { |
| 125 | + data, x, y, remove_na = TRUE, color = NA, palette = NA, both_axes = FALSE, alternate_axes = FALSE, switch_axes = FALSE, call = caller_env()) { |
126 | 126 |
|
127 | 127 | # do we have a data frame? |
128 | 128 | if (missing(data) || !is.data.frame(data)) { |
129 | | - abort("`data` must be a data frame or tibble.") |
| 129 | + cli_abort("`data` must be a data frame or tibble.", call = call) |
130 | 130 | } |
131 | 131 |
|
132 | 132 | # do x and y evaluate correctly? |
133 | 133 | x <- try_fetch( |
134 | 134 | tidyselect::eval_select(rlang::enexpr(x), data), |
135 | 135 | error = function(cnd) { |
136 | | - abort( |
| 136 | + cli_abort( |
137 | 137 | "`x` must be a valid tidyselect expression.", |
138 | | - parent = cnd |
| 138 | + parent = cnd, call = call |
139 | 139 | ) |
140 | 140 | } |
141 | 141 | ) |
142 | 142 | y <- try_fetch( |
143 | 143 | tidyselect::eval_select(rlang::enexpr(y), data), |
144 | 144 | error = function(cnd) { |
145 | | - abort( |
| 145 | + cli_abort( |
146 | 146 | "`y` must be a valid tidyselect expression.", |
147 | | - parent = cnd |
| 147 | + parent = cnd, call = call |
148 | 148 | ) |
149 | 149 | } |
150 | 150 | ) |
151 | 151 |
|
152 | 152 | # do we have at least 1 x and 1 y? |
153 | 153 | if (length(x) < 1 || length(y) < 1) { |
154 | | - abort(c( |
| 154 | + cli_abort(c( |
155 | 155 | "insufficient number of columns", |
156 | 156 | "x" = if (length(x) < 1) "no `x` column selected", |
157 | 157 | "x" = if (length(y) < 1) "no `y` column selected" |
158 | | - )) |
| 158 | + ), call = call) |
159 | 159 | } |
160 | 160 | # do we have both multiple x AND y? |
161 | 161 | if (length(x) > 1 && length(y) > 1) { |
162 | | - abort(c( |
| 162 | + cli_abort(c( |
163 | 163 | "too many columns, only x OR y can select multiple columns", |
164 | 164 | "x" = if (length(x) < 1) "no `x` column selected", |
165 | 165 | "x" = if (length(y) < 1) "no `y` column selected" |
166 | | - )) |
| 166 | + ), call = call) |
167 | 167 | } |
168 | 168 |
|
169 | 169 | # do we have valid remove_na, both_axes, alternate_axes, and switch_axes (the booleans) |
@@ -204,15 +204,15 @@ create_stackplot_tibble <- function( |
204 | 204 | # do we have a valid length for color or palette? |
205 | 205 | stopifnot("can only set either `color` or `palette`, not both" = is.na(color) | is.na(palette)) |
206 | 206 | if (!(is.character(color) || all(is.na(color))) || !length(color) %in% c(1L, nrow(config))) { |
207 | | - abort(sprintf("`color` must be either a single color or one for each variable (%d)", nrow(config))) |
| 207 | + cli_abort(sprintf("`color` must be either a single color or one for each variable (%d)", nrow(config)), call = call) |
208 | 208 | } |
209 | 209 | if (!all(is.na(palette))) { |
210 | 210 | # palette argument provided |
211 | 211 | if (is_scalar_character(palette) && palette %in% rownames(RColorBrewer::brewer.pal.info) && RColorBrewer::brewer.pal.info[palette, 1] >= nrow(config)) { |
212 | 212 | color = RColorBrewer::brewer.pal(RColorBrewer::brewer.pal.info[palette, 1], palette)[1:nrow(config)] |
213 | 213 | } else |
214 | 214 | sprintf("`palette` must be a string identifying a valid RColorBrewer palette with at least %d colors. Use `RColorBrewer::display.brewer.all()` to see all available palettes.", nrow(config)) |> |
215 | | - abort() |
| 215 | + cli_abort(call = call) |
216 | 216 | } |
217 | 217 |
|
218 | 218 |
|
@@ -290,28 +290,33 @@ assemble_stackplot <- function(prepared_stackplot, overlap = 0, simplify_shared_ |
290 | 290 | } |
291 | 291 |
|
292 | 292 | # internal function to great a list of gtables for the combined plot |
293 | | -create_stackplot_gtables <- function(prepared_stackplot, overlap, simplify_shared_axis, shared_axis_size) { |
| 293 | +create_stackplot_gtables <- function(prepared_stackplot, overlap, simplify_shared_axis, shared_axis_size, call = caller_env()) { |
294 | 294 |
|
295 | 295 | # do we have a data frame? |
296 | 296 | req_cols <- c(".var", "config", "data", "plot", "theme") |
297 | 297 | if (missing(prepared_stackplot) || !is.data.frame(prepared_stackplot) || |
298 | 298 | !all(req_cols %in% names(prepared_stackplot))) { |
299 | | - abort( |
300 | | - sprintf("`prepared_stackplot` must be a data frame or tibble with columns '%s'", paste(req_cols, collapse = "', '")) |
| 299 | + cli_abort( |
| 300 | + "{.var prepared_stackplot} must be a data frame or tibble with columns |
| 301 | + {.emph {req_cols}}", call = call |
301 | 302 | ) |
302 | 303 | } |
303 | 304 |
|
304 | 305 | # do we have a valid overlap value? |
305 | 306 | if (missing(overlap) || !is.numeric(overlap) || !all(overlap >= 0) || !all(overlap <= 1) || |
306 | 307 | !length(overlap) %in% c(1L, nrow(prepared_stackplot) - 1L)) { |
307 | | - abort(sprintf("`overlap` must be either a single numeric value (between 0 and 1) or one for each sequential plot overlap (%d)", |
308 | | - nrow(prepared_stackplot) - 1L)) |
| 308 | + cli_abort( |
| 309 | + c("{.var overlap} must be either a single numeric value (between 0 and 1) |
| 310 | + or a vector with {nrow(prepared_stackplot) - 1L} numbers, one for the |
| 311 | + overlap of each sequential plot", |
| 312 | + "x" = "{.var overlap} is a {.obj_type_friendly {overlap}}"), |
| 313 | + call = call) |
309 | 314 | } |
310 | 315 |
|
311 | 316 | # combine plots and themes and assembel the gtables |
312 | 317 | gtables <- prepared_stackplot |> |
313 | 318 | combine_plot_theme_add(simplify_shared_axis = simplify_shared_axis, include_adds = TRUE) |> |
314 | | - tidyr::unnest(.data$config) |> |
| 319 | + tidyr::unnest("config") |> |
315 | 320 | dplyr::select(".var", ".direction", "plot_w_theme") |> |
316 | 321 | # could think about relative sizing here with size_adjust but that doesn't seem like a feature we need |
317 | 322 | dplyr::mutate( |
|
0 commit comments