From a851f363e56af257b753ded26567d086bc3fa9e8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 19 Jul 2023 22:31:25 +0200 Subject: [PATCH] Override default density method --- tests/testthat/helper-density.R | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/testthat/helper-density.R diff --git a/tests/testthat/helper-density.R b/tests/testthat/helper-density.R new file mode 100644 index 0000000000..1896437b8f --- /dev/null +++ b/tests/testthat/helper-density.R @@ -0,0 +1,21 @@ + +# In R devel from 4.3.0 onwards, the density calculation has slightly changed, +# which affects visual snapshots that use a density calculation, like +# `geom_violin()` and `geom_density()`. +# See https://developer.r-project.org/blosxom.cgi/R-devel/NEWS/2023/05/03#n2023-05-03 +# +# It has a backwards compatibility argument called 'old.coords' that can be used +# to perform the classic density calculation, which means we can stably use +# visual tests in R devel. +# +# Since that argument is not available in older versions, we have to use the +# following workaround. Here, we conditionally override the default +# density method to use `old.coords = TRUE`. +if ("old.coords" %in% names(formals(stats::density.default))) { + registerS3method( + "density", "default", + function(..., old.coords = TRUE) { + stats::density.default(..., old.coords = old.coords) + } + ) +}