From 6dae2305860086dc5389cd6eee44ca93a88c3c18 Mon Sep 17 00:00:00 2001 From: Abdessabour Moutik Date: Fri, 8 Oct 2021 12:59:25 -0700 Subject: [PATCH 1/2] Closes #1872. Implemented to_basic for `geom_function` --- R/layers2traces.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/layers2traces.R b/R/layers2traces.R index 0387d34c5c..7bacb539d5 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -170,6 +170,12 @@ to_basic <- function(data, prestats_data, layout, params, p, ...) { UseMethod("to_basic") } +#' @export +to_basic.GeomFunction <- function (data, prestats_data, layout, params, p, ...) { + data$y <- params$fun(data$x) + prefix_class(data, "GeomPath") +} + #' @export to_basic.GeomCol <- function(data, prestats_data, layout, params, p, ...) { prefix_class(data, "GeomBar") From a6281379e9b7a786d1b29d702eb6de71bf4b08a9 Mon Sep 17 00:00:00 2001 From: Abdessabour Moutik Date: Fri, 8 Oct 2021 16:13:47 -0700 Subject: [PATCH 2/2] Added unit tests. And ran tests using --- .../_snaps/ggplot-function/geomfunction.svg | 1 + .../_snaps/ggplot-function/stat-function.svg | 1 + tests/testthat/test-ggplot-function.R | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/testthat/_snaps/ggplot-function/geomfunction.svg create mode 100644 tests/testthat/_snaps/ggplot-function/stat-function.svg create mode 100644 tests/testthat/test-ggplot-function.R diff --git a/tests/testthat/_snaps/ggplot-function/geomfunction.svg b/tests/testthat/_snaps/ggplot-function/geomfunction.svg new file mode 100644 index 0000000000..7c6eebb138 --- /dev/null +++ b/tests/testthat/_snaps/ggplot-function/geomfunction.svg @@ -0,0 +1 @@ +123451020xy diff --git a/tests/testthat/_snaps/ggplot-function/stat-function.svg b/tests/testthat/_snaps/ggplot-function/stat-function.svg new file mode 100644 index 0000000000..fe783e2eb8 --- /dev/null +++ b/tests/testthat/_snaps/ggplot-function/stat-function.svg @@ -0,0 +1 @@ +123451020xy diff --git a/tests/testthat/test-ggplot-function.R b/tests/testthat/test-ggplot-function.R new file mode 100644 index 0000000000..738260f21d --- /dev/null +++ b/tests/testthat/test-ggplot-function.R @@ -0,0 +1,18 @@ +p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x)) + +test_that("ggplotly correctly handles stat_function", { + pl <- p + + stat_function(fun = function(x) x^2+3) + + xlim(1, 5) + + expect_doppelganger(ggplotly(pl), "stat-function") +}) + +test_that("ggplotly correctly handles stat_function", { + pl <- p + + geom_function(fun = function(x) x^2+3) + + stat_function(fun = function(x) 5) + + xlim(1, 5) + + expect_doppelganger(ggplotly(pl), "geomfunction") +}) \ No newline at end of file