Skip to content

Commit

Permalink
Fix #290
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Aug 8, 2023
1 parent 164779d commit fac2ec1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# patchwork (development version)

* `NULL` can now be used with the different arithmetic operators and will result
in a non-operation (i.e. the non-null part will be returned unmodified) (#290)

# patchwork 1.1.2

* Better error message if rendering fails due to too small plotting space
Expand Down
8 changes: 8 additions & 0 deletions R/arithmetic.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ NULL
#' @rdname plot_arithmetic
#' @export
"-.ggplot" <- function(e1, e2) {
if (is.null(e2)) return(e1)
if (is.null(e1)) return(e2)
if (should_autowrap(e2)) e2 <- wrap_elements(full = e2)
if (!is.ggplot(e2)) stop("Only knows how to fold ggplot objects together", call. = FALSE)
patchwork <- new_patchwork()
Expand All @@ -80,6 +82,8 @@ NULL
#' @rdname plot_arithmetic
#' @export
"/.ggplot" <- function(e1, e2) {
if (is.null(e2)) return(e1)
if (is.null(e1)) return(e2)
if (should_autowrap(e2)) e2 <- wrap_elements(full = e2)
if (!is_patchwork(e1)) {
e1 + e2 + plot_layout(ncol = 1)
Expand All @@ -93,6 +97,8 @@ NULL
#' @rdname plot_arithmetic
#' @export
"|.ggplot" <- function(e1, e2) {
if (is.null(e2)) return(e1)
if (is.null(e1)) return(e2)
if (should_autowrap(e2)) e2 <- wrap_elements(full = e2)
if (!is_patchwork(e1)) {
e1 + e2 + plot_layout(nrow = 1)
Expand All @@ -105,6 +111,7 @@ NULL
#' @rdname plot_arithmetic
#' @export
"*.gg" <- function(e1, e2) {
if (is.null(e2)) return(e1)
if (is_patchwork(e1)) {
e1$patches$plots <- lapply(e1$patches$plots, function(p) {
if (!is_patchwork(p)) p <- p + e2
Expand All @@ -117,6 +124,7 @@ NULL
#' @importFrom ggplot2 is.theme
#' @export
"&.gg" <- function(e1, e2) {
if (is.null(e2)) return(e1)
if (is_patchwork(e1)) {
if (is.theme(e2)) {
e1$patches$annotation$theme <- e1$patches$annotation$theme + e2
Expand Down

0 comments on commit fac2ec1

Please sign in to comment.