Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Polkas committed Nov 14, 2024
1 parent 1d8a5aa commit fed7a2a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Depends:
RoxygenNote: 7.3.2
Suggests:
backports,
patchwork,
styler,
testthat (>= 3.0.0)
Config/testthat/edition: 3
8 changes: 3 additions & 5 deletions R/ggcall.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
1 change: 1 addition & 0 deletions R/patchwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
}
plot
}

#' @importFrom grid is.grob
#' @rdname plot_arithmetic
#' @export
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_eval_ggcall.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
Expand Down Expand Up @@ -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")
})
5 changes: 2 additions & 3 deletions tests/testthat/test_ggcall.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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"))
})

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) {
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_patchwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})

0 comments on commit fed7a2a

Please sign in to comment.