Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions R/all-classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class_ggplot <- S7::new_class(
layout = class_layout,
labels = class_labels,
meta = S7::class_list,
version = S7::new_S3_class("package_version"),
plot_env = S7::class_environment
),
constructor = function(
Expand Down Expand Up @@ -368,6 +369,7 @@ class_ggplot <- S7::new_class(
layout = layout %||% ggproto(NULL, Layout),
labels = labels,
meta = meta,
version = current_version,
plot_env = plot_env
)
}
Expand Down
8 changes: 8 additions & 0 deletions R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ ggplot_build <- function(plot, ...) {
if (!is.null(env)) {
attach_plot_env(env)
}
if (!identical(try_prop(plot, "version"), current_version)) {
cli::cli_abort(c(
"The plot object was created with a different version of ggplot2. \\
Please reconstruct the plot.",
i = "Did you save the plot object to disk?"
))
}

UseMethod("ggplot_build")
}

Expand Down
3 changes: 3 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ on_load(
if (getRversion() >= "4.3.0") registerS3method("+", "gg", add_gg)
)

current_version <- NULL
on_load(current_version <- packageVersion("ggplot2"))

on_load(S7::methods_register())
.onLoad <- function(...) {
run_on_load()
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/plot-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# attempting the render plot build with previous version fails

Code
print(p)
Condition
Error in `ggplot_build()`:
! The plot object was created with a different version of ggplot2. Please reconstruct the plot.
i Did you save the plot object to disk?

6 changes: 6 additions & 0 deletions tests/testthat/test-plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ test_that("strings are not converted to factors", {

expect_type(get_layer_data(p)$label, "character")
})

test_that("attempting the render plot build with previous version fails", {
p <- ggplot(mtcars, aes(disp, mpg)) + geom_point()
p@version <- package_version("3.5.2")
expect_snapshot(print(p), error = TRUE)
})
Loading