-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow using functions that return NULL in aes() #2997
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
Allow using functions that return NULL in aes() #2997
Conversation
tests/testthat/test-layer.r
Outdated
@@ -38,6 +38,13 @@ test_that("missing aesthetics trigger informative error", { | |||
) | |||
}) | |||
|
|||
test_that("if an aes is mapped to a function that returns NULL, it is removed", { | |||
df <- data.frame(x = 1:10) | |||
wrap <- function(...) tryCatch(..., error = function(e) NULL) |
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 think you can simplify this to just function() NULL
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.
Agreed.
Can you please add a bullet to NEWS? It should briefly describe the change and end with |
Thanks! I tend to forget about NEWS bullets, sorry. |
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/ |
It's common practice to wrap unsafe calculations with
tryCatch()
and returnNULL
, which allows us to use the result when available and skip when unavailable. But, currently, ggplot2 doesn't accept this inaes()
:This is because
Layer$compute_aesthetics()
removesNULL
aes here BEFORE evaluation:ggplot2/R/layer.r
Lines 221 to 223 in f5a88a7
Considering the following facts, I think it's safe to move
compact()
after evaluationggplot2::compact()
removes onlyNULL
NULL
will be stillNULL
after evaluation