Skip to content
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

Warn on the use of ..var.. and stat(var) #4950

Merged

Conversation

yutannihilation
Copy link
Member

Fix #3693

Considering the wide use of them, now I feel it's a bit difficult to deprecate them. Still, I think it's a good idea to issue a non-warning message at least because we want to advertise after_stat() more.

@thomasp85
Copy link
Member

I think this is sensible - @hadley do you have any issues with this?

Also, @yutannihilation is there a reason why you are not using the lifecycle::deprecate_soft() functionality for this?

@yutannihilation
Copy link
Member Author

why you are not using the lifecycle::deprecate_soft() functionality

The main reason is that I just copied the code from here. Actually, I'm not sure when to use lifecycle::* functions and when to use cli::* functions... But, in this case, I think "lifecycle::deprecate_soft()" is not suitable because ..var.. and stat(var) is not deprecated (NEWS.md says they are "superseded").

ggplot2/R/layer.r

Lines 132 to 136 in 7ac55dd

# TODO: move to cli_warn()
cli::cli_inform(c(
"{.field size} aesthetic has been deprecated for use with lines as of ggplot2 3.4.0",
"i" = "Please use {.field linewidth} aesthetic instead"
), .frequency = "regularly", .frequency_id = "ggplot-size-linewidth")

@thomasp85
Copy link
Member

Ah, I see... The reason why we don't use it in the example above is that the auto-generation of text doesn't really capture what is happening due to the special syntax of ggplot2

In general we will always use the lifecycle functions for deprecations. If they have been superseded I don't think we should issue any warning at all since that means they are still proper syntax

I guess the question then is: Do we want to formally deprecate them? If so we should use lifecycle

@yutannihilation
Copy link
Member Author

I see, thanks for the explanation!

If they have been superseded I don't think we should issue any warning at all since that means they are still proper syntax

I guess the question then is: Do we want to formally deprecate them? If so we should use lifecycle

Makes sense. In my opinion, we should deprecate them then. A superseded function or syntax is preserved because it might not be very easy for all users to replace it with the superseder one. But, in this case, all after_stat(var), ..var.., and stat(var) refer to the same thing with different names. I'll wait for Hadley's comment.

@hadley
Copy link
Member

hadley commented Aug 23, 2022

I think it's probably ok to deprecate them; the warning is only once every 8 hours so it's not terribly annoying.

Note that in dev lifecycle you can use I() in what and reason to generate your own text for this sort of situation. lifecycle will be going to CRAN soon, so I think it's ok to temporarily depend on dev lifecycle to get access to this functionality.

@yutannihilation
Copy link
Member Author

Thanks! Then, let's move forward.

But, I found lifecycle::deprecate_soft() doesn't fit in this case because both ..var.. and stat(var) are not functions that are called directly, but are just expressions evaluated non-standardly. My guess is that this was also the true reason why you chose cli_inform() in linewidth's case. Am I correct?

Let me raise one more question. If the intention is to mimic the behaviour of lifecycle::deprecate_soft(), shouldn't we use cli::cli_warn() instead of cli::cli_inform()? (I mean both in this pull request and in the linewidth's case)

@hadley
Copy link
Member

hadley commented Aug 24, 2022

See https://lifecycle.r-lib.org/dev/articles/communicate.html#anything-else

I’d also recommend using deprecate_warn() as soft deprecation will only affect package developers (which I don’t think is what you want).

@thomasp85
Copy link
Member

@yutannihilation you can easily use lifecycle for deprecate_warn("3.4.0", "stat()", "after_stat()")... it's a bit more involved to use it for ..var.. so there we should rely on dev lifecycle

@yutannihilation
Copy link
Member Author

yutannihilation commented Aug 24, 2022

@hadley
Thanks, you are right. This deprecation targets the end users. It makes sense to skip _soft() stage.

@thomasp85
I see, thanks. I'll use I() for the dot-dot notation.

@hadley
Copy link
Member

hadley commented Aug 24, 2022

If you don't mind, could you also update the line size deprecation to use the new lifecycle feature?

@yutannihilation
Copy link
Member Author

Sure!

@yutannihilation
Copy link
Member Author

devtools::load_all("~/GitHub/ggplot2/")
#> ℹ Loading ggplot2

p <- ggplot(data.frame(), aes(..x.., stat(y)))
b <- ggplot_build(p)
#> Warning: The dot-dot notation (`..var..`) was deprecated in ggplot2 3.4.0.
#> Please use `after_stat()` instead.
#> Warning: `stat()` was deprecated in ggplot2 3.4.0.
#> Please use `after_stat()` instead.

Created on 2022-08-24 with reprex v2.0.2

is_dotted_var(as.character(x))
res <- is_dotted_var(as.character(x))
if (res && warn) {
# TODO: if we want to set always = TRUE, we probably need some mechanism
Copy link
Member

Choose a reason for hiding this comment

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

I think you can remove this comment; we're still some time away from setting always = TRUE, if we ever do so. This is a very old feature, so I think the deprecation needs to be quite mild.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

Comment on lines +102 to +104
p <- ggplot(pts)
expect_warning(
expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)),
expect_identical(grob_xy_length(p + geom_sf(aes(size = size))), c(1L, 1L)),
Copy link
Member Author

Choose a reason for hiding this comment

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

I needed a tweak here because of this issue: #4960

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

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

Could you please add a snapshot test that shows both new warnings?

is_dotted_var(as.character(x))
res <- is_dotted_var(as.character(x))
if (res && warn) {
lifecycle::deprecate_warn("3.4.0", I("The dot-dot notation (`..var..`)"), "after_stat()")
Copy link
Member

Choose a reason for hiding this comment

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

Could we interpolate the variable name in to ..{{var}}.. and after_stat({var}})? (assuming var <- as.character(x)))

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean this is supposed to work...?

var <- "foo"
lifecycle::deprecate_warn("3.4.0", "a()", "b({{var}})")
#> Error in `as_string()`:
#> ! Can't convert a call to a string.

#> Backtrace:
#>     ▆
#>  1. └─lifecycle::deprecate_warn("3.4.0", "a()", "b({{var}})")
#>  2.   ├─id %||% msg
#>  3.   └─lifecycle:::lifecycle_message(...)
#>  4.     └─lifecycle:::spec(with, NULL, signaller)
#>  5.       └─lifecycle:::spec_arg(what$call, signaller = signaller)
#>  6.         └─rlang::as_string(node_car(arg))
#>  7.           └─rlang:::abort_coercion(x, "a string")
#>  8.             └─rlang::abort(msg, call = call)

Created on 2022-08-25 with reprex v2.0.2

Copy link
Member

Choose a reason for hiding this comment

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

Oh sorry, you’ll need to glue/paste yourself.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. Thanks for the suggestion anyway!

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

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

Looks great!

@yutannihilation
Copy link
Member Author

Thanks!

@thomasp85
Do you have further comments?

@thomasp85
Copy link
Member

LGTM

@yutannihilation
Copy link
Member Author

Thanks!

@yutannihilation yutannihilation merged commit 6434c1e into tidyverse:main Aug 25, 2022
@yutannihilation yutannihilation deleted the feature/warn-old-calculated-aes branch August 25, 2022 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn on stat() and ..var..-notation?
3 participants