-
Notifications
You must be signed in to change notification settings - Fork 2.1k
rlang/purrr style anonymous function specification in stat_function #3160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rlang/purrr style anonymous function specification in stat_function #3160
Conversation
…/ggplot2 into purrr-style-stat-function
R/ggplot2.r
Outdated
@@ -3,5 +3,5 @@ | |||
|
|||
#' @import scales grid gtable | |||
#' @importFrom stats setNames | |||
#' @importFrom rlang quo quos | |||
#' @importFrom rlang quo quos as_function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you use rlang::
, you don't need to import this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't we need it somewhere to avoid CRAN warnings about not being imported? Otherwise, I'm fine removing of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. I suggest removing. If it's a problem the Travis build will break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? As long as you use fully qualified form (rlang::as_function()
), you don't need to import.
R/stat-function.r
Outdated
@@ -5,7 +5,7 @@ | |||
#' the x axis, and the results are drawn (by default) with a line. | |||
#' | |||
#' @eval rd_aesthetics("stat", "function") | |||
#' @param fun function to use. Must be vectorised. | |||
#' @param fun function to use or formula. Must be vectorised. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please expand this sentence a bit more. Now that we have two different possible inputs, the documentation needs a little more care. Also, please add a simple example to the examples, or maybe better rewrite one or two of the current ones (so we don't increase the total number of examples).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The .x
in the formula should also be explained here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we also need a link to as_function
's doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to update the docs for the argument and update the examples more fully, if you'd like, I was just trying to be as minimalist as possible. Want me to use .
or .x
for the formula stuff? (@yutannihilation brought the .
vs .x
part up in another comment, too.)
R/stat-function.r
Outdated
@@ -49,6 +49,7 @@ | |||
#' # Using a custom function | |||
#' test <- function(x) {x ^ 2 + x + 20} | |||
#' f + stat_function(fun = test) | |||
#' f + stat_function(fun = ~ .x^2 + .x + 20) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, .
is better for single-argument function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, too: .x
seems to be more tidyverse consistent in that it's more referentially transparent in a piping context, but piping is not native to ggplot2. I don't have a strong preference either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a slight preference for .x
, because I think it imposes slightly lower mental load, in particular for complex formulas. Compare:
fun = ~ (5*.^2 + 3*.) * exp(-2*.^2 + 3*. + 2)
fun = ~ (5*.x^2 + 3*.x) * exp(-2*.x^2 + 3*.x + 2)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I prefer .
because it's short, but it's just my preference and I don't have strong opinion. Let's use .x
.
(Note that in some places e.g. sec_axis()
.x
is not allowed, but it's just implementational details.)
R/stat-function.r
Outdated
@@ -5,7 +5,7 @@ | |||
#' the x axis, and the results are drawn (by default) with a line. | |||
#' | |||
#' @eval rd_aesthetics("stat", "function") | |||
#' @param fun function to use. Must be vectorised. | |||
#' @param fun function to use or formula. Must be vectorised. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we also need a link to as_function
's doc.
I think I've included the updates in some new commits (dkahle/ggplot2@0e1a56f and dkahle/ggplot2@612b6d1), how can I add those to this PR? Or, do I need to submit a new PR with all of them? |
R/stat-function.r
Outdated
@@ -16,39 +19,44 @@ | |||
#' \item{x}{x's along a grid} | |||
#' \item{y}{value of function evaluated at corresponding x} | |||
#' } | |||
#' @seealso \code{\link[rlang:as_function]{as_function()}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be rlang::as_function
? (two colons instead of one)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought in the link you put a single colon; works in the docs as I built them, at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single colon is correct.
c.f. https://cran.r-project.org/doc/manuals/R-exts.html#Cross_002dreferences
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was not accurate above. A single colon is correct as a raw Rd
code, but we should use Markdown-style as we do so in other places: [rlang::as_function]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks right with parentheses. (I'm seeing more like [rlang::as_function()]
.) Got it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, [rlang::as_function()]
is correct, sorry. More details are here: https://cran.r-project.org/web/packages/roxygen2/vignettes/markdown.html#links
The new commits get added automatically. Note that the travis build fails with R devel. Not exactly sure from a quick glance what the problem is. Could you check if you can identify the issue? |
One more issue from my side: Maybe remove 2 or 3 of the examples. Examples are somewhat expensive, because they all get executed on every CRAN check. The current version has 7 plots, which is already a lot. |
Cleaned the docs a bit. Looking into the R-devel issue. It's been flagging on me in some other projects, too, so I'm not sure what's going on there. |
It seems R devel is currently broken; I believe #3161 is a harmless change, but it also fails only on r-devel. https://travis-ci.org/tidyverse/ggplot2/jobs/497352631 We might need to look into it, but I think it's irrelevant to this PR. |
The R-devel issue was solved by #3163. |
@dkahle Could you remove |
@clauswilke I'm merging this. The number of examples might still needs to be reduced, but I think we can restructure the examples when ggplot2 gets ready to promote |
Thanks! |
@yutannihilation Ok. Maybe open an issue about this so we don't forget? |
Good idea, thanks. Here it is: #3181 |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Closes #3159.