Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make patchwork arithmetic operators extendable #310

Open
ddsjoberg opened this issue Nov 7, 2022 · 0 comments
Open

Make patchwork arithmetic operators extendable #310

ddsjoberg opened this issue Nov 7, 2022 · 0 comments
Labels
feature a feature request or enhancement

Comments

@ddsjoberg
Copy link

Hej hej @thomasp85 !

Thank you for the wonderful pkg. I use it often and it's super helpful 馃

The first line of code in the arithmetic operator functions is if (should_autowrap(e2)) e2 <- wrap_elements(full = e2). The should_autowrap() function is simple, and makes it possible to apply the wrap_elements() function to gobs that require it.

should_autowrap <- function(x) {
  is.grob(x) || inherits(x, 'formula') || is.raster(x) || inherits(x, 'nativeRaster')
}

I maintain a ggplot-based package for creating survival curves (https://github.com/ddsjoberg/ggsurvfit), and I would love to be able to utilize the patchwork arithmetic operators. To make this work, I would need to perform some pre-processing for my class of plot. To make the patchwork arithmetic operators extendable, we could update this line if (should_autowrap(e2)) e2 <- wrap_elements(full = e2) to be a generic instead.

Here's what I had in mind:

#' @export
#' @rdname arithmetic_prep
arithmetic_prep <- function(x, ...) {
  UseMethod("arithmetic_prep")
}

#' @importFrom grid is.grob
#' @importFrom grDevices is.raster
#' @export
#' @rdname arithmetic_prep
arithmetic_prep.default <- function(x, ...) {
  # wrap input plot as needed
  if (is.grob(x) || inherits(x, 'formula') || is.raster(x) || inherits(x, 'nativeRaster')) {
    x <- wrap_elements(full = x)
  }
  x
}

By making this a generic function, other package authors could write their own methods to properly handle plots of other classes. If you're amenable to this, I can prepare a pull request. Thanks! 馃悷

@thomasp85 thomasp85 added the feature a feature request or enhancement label Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants