From fed7a2a4eed61da94311db55db357e071a965d73 Mon Sep 17 00:00:00 2001 From: Maciej Nasinski Date: Thu, 14 Nov 2024 10:47:35 +0100 Subject: [PATCH] fix --- DESCRIPTION | 1 + R/ggcall.R | 8 +++----- R/patchwork.R | 1 + tests/testthat/test_eval_ggcall.R | 4 ++-- tests/testthat/test_ggcall.R | 5 ++--- tests/testthat/test_patchwork.R | 12 ++++++++++++ 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9f58752..83d116b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,6 +20,7 @@ Depends: RoxygenNote: 7.3.2 Suggests: backports, + patchwork, styler, testthat (>= 3.0.0) Config/testthat/edition: 3 diff --git a/R/ggcall.R b/R/ggcall.R index fddadee..6106773 100644 --- a/R/ggcall.R +++ b/R/ggcall.R @@ -50,21 +50,19 @@ ggplot <- function(...) { #' `+.gg` <- function(e1, e2) { validate_ggplot() + validate_patchwork() plot <- utils::getFromNamespace("+.gg", "ggplot2")(e1, e2) if (inherits(e1, "ggcall")) { if (inherits(e2, "ggcall")) { - history <- call("(", ggcall(e2)) + attr(plot, "ggcall") <- bquote(.(ggcall(e1)) + .(call("(", ggcall(e2)))) } else { history <- substitute(e2) + attr(plot, "ggcall") <- bquote(.(ggcall(e1)) + .(history)) } - - attr(plot, "ggcall") <- bquote(.(attr(plot, "ggcall")) + .(history)) - if (!identical(attr(e1, "ggcall_env_last"), parent.frame())) { attr(plot, "ggcall_env") <- merge_env(attr(plot, "ggcall_env"), parent.frame()) } - attr(plot, "ggcall_env_last") <- parent.frame() } diff --git a/R/patchwork.R b/R/patchwork.R index edc03f0..f06d101 100644 --- a/R/patchwork.R +++ b/R/patchwork.R @@ -9,6 +9,7 @@ } plot } + #' @importFrom grid is.grob #' @rdname plot_arithmetic #' @export diff --git a/tests/testthat/test_eval_ggcall.R b/tests/testthat/test_eval_ggcall.R index a487635..9f32a65 100644 --- a/tests/testthat/test_eval_ggcall.R +++ b/tests/testthat/test_eval_ggcall.R @@ -67,7 +67,7 @@ test_that("eval_ggcall reproduces the plot", { plot_call <- ggcall(p) reconstructed_plot <- eval_ggcall(plot_call) expect_true(inherits(reconstructed_plot, "ggplot")) - expect_length(attr(reconstructed_plot, "ggcall"), 2) + expect_type(attr(reconstructed_plot, "ggcall"), "language") original_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(alpha = 0.4) + @@ -125,5 +125,5 @@ testthat::test_that("eval_ggcall works with ellipsis", { new_plot <- eval_ggcall(plot_call, mtcars = mtcars2) expect_identical(nrow(new_plot$data), 10L) expect_true(inherits(new_plot, "ggplot")) - expect_length(attr(new_plot, "ggcall"), 2) + expect_type(attr(new_plot, "ggcall"), "language") }) diff --git a/tests/testthat/test_ggcall.R b/tests/testthat/test_ggcall.R index 8a7d867..6617572 100644 --- a/tests/testthat/test_ggcall.R +++ b/tests/testthat/test_ggcall.R @@ -1,8 +1,7 @@ test_that("custom ggplot function initializes history", { p <- ggplot(mtcars, aes(x = wt, y = mpg)) expect_s3_class(p, "ggcall") - expect_type(attr(p, "ggcall"), "list") - expect_length(attr(p, "ggcall"), 1) + expect_type(attr(p, "ggcall"), "language") expect_true(inherits(attr(p, "ggcall_env"), "environment")) expect_true(inherits(attr(p, "ggcall_env_last"), "environment")) }) @@ -10,7 +9,7 @@ test_that("custom ggplot function initializes history", { test_that("custom '+' operator appends history", { p <- ggplot(mtcars, aes(x = wt, y = mpg)) p <- p + geom_point() - expect_length(attr(p, "ggcall"), 2) + expect_type(attr(p, "ggcall"), "language") }) func <- function(x, y) { diff --git a/tests/testthat/test_patchwork.R b/tests/testthat/test_patchwork.R index a0e233d..818003f 100644 --- a/tests/testthat/test_patchwork.R +++ b/tests/testthat/test_patchwork.R @@ -24,4 +24,16 @@ test_that("patchwork + operator", { ) ) expect_true(is.ggplot(eval_ggcall(plot))) + + plot <- ggcall(p1 / p2 - p3) + deplot <- backports:::deparse1(plot) + expect_identical( + deplot, + backports:::deparse1( + quote((ggplot(mtcars) + geom_point(aes(mpg, disp)))/(ggplot(mtcars) + + geom_boxplot(aes(gear, disp, group = gear))) - (ggplot(mtcars) + geom_bar(aes(gear)) + facet_wrap(~cyl)) + ) + ) + ) + expect_true(is.ggplot(eval_ggcall(plot))) })