Skip to content

Commit

Permalink
fix stat_function() regression (#4016)
Browse files Browse the repository at this point in the history
* fix stat_function() regression

* fix test
  • Loading branch information
clauswilke authored May 21, 2020
1 parent dbbcd43 commit 4826838
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
6 changes: 1 addition & 5 deletions R/geom-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@
geom_function <- function(mapping = NULL, data = NULL, stat = "function",
position = "identity", ..., na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE) {
# Warn if supplied data is going to be overwritten
if (identical(stat, "function")) {
if (!is.null(data)) {
warn("`data` is not used by stat_function()")
}
if (is.null(data)) {
data <- ensure_nonempty_data
}

Expand Down
7 changes: 2 additions & 5 deletions R/stat-function.r
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ stat_function <- function(mapping = NULL, data = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {

# Warn if supplied data is going to be overwritten
if (!is.null(data)) {
warn("`data` is not used by stat_function()")
if (is.null(data)) {
data <- ensure_nonempty_data
}
data <- ensure_nonempty_data

layer(
data = data,
Expand Down
33 changes: 30 additions & 3 deletions tests/testthat/test-stat-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,34 @@ test_that("Warn when drawing multiple copies of the same function", {
expect_warning(f(), "Multiple drawing groups")
})

test_that("`data` is not used by stat_function()", {
expect_warning(geom_function(data = mtcars, fun = identity), "`data` is not used")
expect_warning(stat_function(data = mtcars, fun = identity), "`data` is not used")
test_that("Line style can be changed via provided data", {
df <- data_frame(fun = "#D55E00")

base <- ggplot(df) +
geom_function(aes(color = fun), fun = identity, n = 6) +
scale_color_identity()
ret <- layer_data(base)
expect_identical(ret$x, seq(0, 1, length.out = 6))
expect_identical(ret$y, ret$x)
expect_identical(ret$colour, rep("#D55E00", 6))

base <- ggplot() +
geom_function(
data = df, aes(color = fun), fun = identity, n = 6
) +
scale_color_identity()
ret <- layer_data(base)
expect_identical(ret$x, seq(0, 1, length.out = 6))
expect_identical(ret$y, ret$x)
expect_identical(ret$colour, rep("#D55E00", 6))

base <- ggplot() +
stat_function(
data = df, aes(color = fun), fun = identity, n = 6
) +
scale_color_identity()
ret <- layer_data(base)
expect_identical(ret$x, seq(0, 1, length.out = 6))
expect_identical(ret$y, ret$x)
expect_identical(ret$colour, rep("#D55E00", 6))
})

0 comments on commit 4826838

Please sign in to comment.