Skip to content

Commit 125cc5f

Browse files
thiyangtlionel-
authored andcommitted
Improve stat_function() documentation and warn informatively (#3415)
Fixes #3362
1 parent a339a18 commit 125cc5f

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

R/geom-point.r

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#' often aesthetics, used to set an aesthetic to a fixed value, like
3636
#' `colour = "red"` or `size = 3`. They may also be parameters
3737
#' to the paired geom/stat.
38-
#' @inheritParams layer
3938
#' @export
4039
#' @examples
4140
#' p <- ggplot(mtcars, aes(wt, mpg))

R/stat-function.r

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#' The function is called with a grid of evenly spaced values along the x axis,
55
#' and the results are drawn (by default) with a line.
66
#'
7-
#' @eval rd_aesthetics("stat", "function")
7+
#'
88
#' @param fun Function to use. Either 1) an anonymous function in the base or
99
#' rlang formula syntax (see [rlang::as_function()])
1010
#' or 2) a quoted or character name referencing a function; see examples. Must
1111
#' be vectorised.
1212
#' @param n Number of points to interpolate along
13-
#' @param args List of additional arguments to pass to `fun`
13+
#' @param args List of additional arguments passed on to the function defined by `fun`.
1414
#' @param xlim Optionally, restrict the range of the function to this range.
1515
#' @inheritParams layer
1616
#' @inheritParams geom_point
@@ -64,6 +64,15 @@ stat_function <- function(mapping = NULL, data = NULL,
6464
na.rm = FALSE,
6565
show.legend = NA,
6666
inherit.aes = TRUE) {
67+
68+
# Warn if supplied mapping and/or data is going to be overwritten
69+
if (!is.null(mapping)) {
70+
warning("`mapping` is not used by stat_function()", call. = FALSE)
71+
}
72+
if (!is.null(data)) {
73+
warning("`data` is not used by stat_function()", call. = FALSE)
74+
}
75+
6776
layer(
6877
data = data,
6978
mapping = mapping,

man/stat_function.Rd

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-stats-function.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ test_that("works with formula syntax", {
4747
expect_equal(ret$x, s)
4848
expect_equal(ret$y, s^2)
4949
})
50+
51+
test_that("`mapping` is not used by stat_function()", {
52+
expect_warning(stat_function(aes(), fun = identity), "`mapping` is not used")
53+
})
54+
55+
test_that("`data` is not used by stat_function()", {
56+
expect_warning(stat_function(data = mtcars, fun = identity), "`data` is not used")
57+
})

0 commit comments

Comments
 (0)