-
Notifications
You must be signed in to change notification settings - Fork 2.1k
WIP: Allow expressions in facet specs even when some variables are not available on some layers #3735
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
WIP: Allow expressions in facet specs even when some variables are not available on some layers #3735
Conversation
Note that this cannot handle data pronoun at the moment. Probably I did wrong about environment ordering and I should define active bindings on the data mask directly. Will fix... ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
geom_vline(xintercept = 20) +
facet_wrap(vars(.data$am))
#> Error: Column `am` not found in `.data` |
Now devtools::load_all("~/repo/ggplot2/")
#> Loading ggplot2
ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
geom_vline(xintercept = 20) +
facet_wrap(vars(.data$am)) Created on 2020-01-16 by the reprex package (v0.3.0) |
I think this is basically ready for review but, since this is not the one we should have in v3.3.0, let's leave this for a while. Any comments are welcome. |
@hadley I know this is likely too big for the upcoming release, but this PR solves (very nicely!) the only qualm I have with recommending With the new |
This seems like an elegant approach to fixing the problem. What are the potential risks to including this in 3.3.0? It feels fairly low risk to me. |
Thanks, I just hesitated to add a new one to the milestones while the hard deadline is approaching, but I'd love to finish this PR if there's still some more time. I don't have any big concern about this change because the most cases will be handled by this existing logic (though I'm not yet sure whether we should keep this or not): if (quo_is_symbol(facet)) {
facet <- as.character(quo_get_expr(facet))
if (facet %in% names(data)) {
out <- data[[facet]]
} else {
out <- NULL
}
return(out)
} Note that this means we might still need library(ggplot2)
p <- ggplot(mpg) + geom_bar(aes(cyl))
# friendly error
p + facet_wrap(vars(no_such_column))
#> Error: At least one layer must contain all faceting variables: `no_such_column`.
#> * Plot is missing `no_such_column`
#> * Layer 1 is missing `no_such_column`
# fine, but unfriendly error
p + facet_wrap(vars(.data$no_such_column))
#> Error: Column `no_such_column` not found in `.data`
# not sure if this should be allowed or not
no_such_column <- 1
p + facet_wrap(vars(no_such_column))
#> Error: At least one layer must contain all faceting variables: `no_such_column`.
#> * Plot is missing `no_such_column`
#> * Layer 1 is missing `no_such_column` @clauswilke @thomasp85 |
I think it's somewhat risky to introduce this kind of change this late in the release process, even if we're pretty sure it'll be fine. A safe approach would be to add the patch to a branch of the release branch, run a revdep check on that, and only merge into the release branch if no new regressions are found. |
Let me give this a thorough review, and I'll contemplate where this might change existing behaviour. I think if it's low risk we can merge, and then re-run the revdeps; if that reveals any problems we can back out and try again for the next version. |
I think this PR is safer than what I was proposing, since it keeps the exact same code for the case of a symbol. |
@yutannihilation I made a couple of tiny style changes, but otherwise this is lovely @clauswilke my analysis suggests that this change should only affect a small set of plots that previously errored. I suggest we merge, and then re-run revdepchecks just to be safe. @thomasp85 does that sound ok with you? I would like to get this into this release so we can over a comprehensive approach to tidyeval that works the same way in ggplot2 and dplyr (1.0.0) |
This comment has been minimized.
This comment has been minimized.
This looks good. I’m fine with trying a merge into the rc branch |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@hadley @thomasp85 |
@hadley Sounds good. |
I forgot that we can't easily modify this PR to merge on the RC, so lets close this PR. Further discussion can happen on #3757 |
Fix #2963
Not all variables in a facet spec are available on all the layers. A solution suggested on r-lib/rlang#888 (comment) is:
This PR injects such an active bindings on the column names of all the plot data so that we can let
eval_facet()
fail gracefully.Note that this PR removes
env
argument ofeval_facet()
to avoid confusion. I believe all facet specs are converted to quosures and bare expressions are not allowed, which meansenv
is always ignored oneval_tidy()
.Created on 2020-01-16 by the reprex package (v0.3.0)
TODO:
warn_for_aes_extract_usage()
here as wellFacet
(I bet it's OK)