Skip to content

rlang/purrr style anonymous function specification in stat_function#3160

Merged
yutannihilation merged 8 commits into
tidyverse:masterfrom
dkahle:purrr-style-stat-function
Mar 8, 2019
Merged

rlang/purrr style anonymous function specification in stat_function#3160
yutannihilation merged 8 commits into
tidyverse:masterfrom
dkahle:purrr-style-stat-function

Conversation

@dkahle
Copy link
Copy Markdown
Contributor

@dkahle dkahle commented Feb 22, 2019

Closes #3159.

Comment thread R/ggplot2.r Outdated
#' @import scales grid gtable
#' @importFrom stats setNames
#' @importFrom rlang quo quos
#' @importFrom rlang quo quos as_function
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

@yutannihilation yutannihilation Feb 23, 2019

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.

Comment thread R/stat-function.r Outdated
#'
#' @eval rd_aesthetics("stat", "function")
#' @param fun function to use. Must be vectorised.
#' @param fun function to use or formula. Must be vectorised.
Copy link
Copy Markdown
Member

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).

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.)

Comment thread tests/testthat/test-stats-function.r Outdated
Comment thread R/stat-function.r Outdated
#' # Using a custom function
#' test <- function(x) {x ^ 2 + x + 20}
#' f + stat_function(fun = test)
#' f + stat_function(fun = ~ .x^2 + .x + 20)
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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)

Copy link
Copy Markdown
Member

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.)

Comment thread R/stat-function.r Outdated
#'
#' @eval rd_aesthetics("stat", "function")
#' @param fun function to use. Must be vectorised.
#' @param fun function to use or formula. Must be vectorised.
Copy link
Copy Markdown
Member

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.

@dkahle
Copy link
Copy Markdown
Contributor Author

dkahle commented Feb 23, 2019

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?

Comment thread R/stat-function.r Outdated
#' \item{x}{x's along a grid}
#' \item{y}{value of function evaluated at corresponding x}
#' }
#' @seealso \code{\link[rlang:as_function]{as_function()}}
Copy link
Copy Markdown
Member

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)

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

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].

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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

@clauswilke
Copy link
Copy Markdown
Member

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?

@clauswilke
Copy link
Copy Markdown
Member

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.

@dkahle
Copy link
Copy Markdown
Contributor Author

dkahle commented Feb 23, 2019

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.

@yutannihilation
Copy link
Copy Markdown
Member

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.

@yutannihilation
Copy link
Copy Markdown
Member

The R-devel issue was solved by #3163.

@yutannihilation
Copy link
Copy Markdown
Member

@dkahle Could you remove as_function from @importFrom? I feel this is ready to merge if you fix it.

@yutannihilation
Copy link
Copy Markdown
Member

@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 ~ notations.

@yutannihilation yutannihilation merged commit ae1c8f0 into tidyverse:master Mar 8, 2019
@yutannihilation
Copy link
Copy Markdown
Member

Thanks!

@clauswilke
Copy link
Copy Markdown
Member

@yutannihilation Ok. Maybe open an issue about this so we don't forget?

@yutannihilation
Copy link
Copy Markdown
Member

Good idea, thanks. Here it is: #3181

@lock
Copy link
Copy Markdown

lock Bot commented Sep 4, 2019

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/

@lock lock Bot locked and limited conversation to collaborators Sep 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: rlang/purrr-style anonymous functions in stat_function

3 participants