-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Make title, subtitle, caption and tag as labs()'s named arguments #2669
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
Make title, subtitle, caption and tag as labs()'s named arguments #2669
Conversation
See also: #2446 |
Oh, thanks. If that is intended, I'm ok with it. |
I don't like it either. |
I think we should make |
R/labels.r
Outdated
args <- rename_aes(args) | ||
|
||
# Since NULL should be preserved, we should wrap args with list() | ||
if (!missing(title)) args["title"] <- list(title) |
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.
@lionel- can you think of a more elegant way to write 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.
Perhaps remove the named arguments and let users pass them through ...
?
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.
That's what we did previously 😄 We're adding them as named params here for better docs + autocomplete.
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.
Hmm I can't think of anything better.
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.
Thanks, then I leave this as is :)
I'm OK with either. But, as @batpigandme pointed on #2446, this behavior of Maybe is it better to leave this as is and encourage the users to use |
I disagree with @batpigandme's reading of the documentation. The current documentation says "Leave I'm with @hadley. Don't override without explicit
I also think the description for
|
Thanks! Sounds reasonable to me. |
For programmability it might be better to use another sentinel value like |
I see. There's a trade off. For here, I want to choose missingness for better documentation. |
Sorry, I'm not sure what is the good way to pass missingness to other function. I'll try, but any advice will be appreciated! |
Done. Thanks @clauswilke for the sentences.
In this case, I could take the advantage of the fact that |
To pass missingness programmatically you need some meta-programming: with_qq <- function(expr) {
expr <- enexpr(expr)
eval_bare(expr, caller_env())
}
f <- function(x) {
missing(x)
}
wrapper <- function(x) {
with_qq(f(!!maybe_missing(x)))
}
wrapper()
#> [1] TRUE
wrapper(1)
#> [1] FALSE |
Thanks! |
^^ @clauswilke this makes sense to me. 👍 |
There's also |
|
Ah, I forgot about |
R/labels.r
Outdated
#' p + labs(title = "title") + labs(title = NULL) | ||
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(), tag = waiver()) { | ||
args <- list(..., title = title, subtitle = subtitle, caption = caption, tag = tag) | ||
if (length(args) > 0 && is.list(args[[1]]) && !is.waive(args[[1]])) args <- args[[1]] |
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.
By the way, why should labs()
accept a list as its first argument?
I couldn't find this is documented or used elsewhere.
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.
Now that ggplot2 uses tidy eval, I think we should remove this, and in the previous line use rlang::list2()
instead of list()
. That way you can use !!!
if you do already have a list of labels.
My guess is to avoid the |
Thanks, if this is useful for some people, I leave this as is. |
I’d be happy to remove that feature if it doesn’t break existing code (but it probably will) |
OK. I just hoped no one knows this feature because it doesn't used internally in ggplot2 and no documents seems to advertise it. But, I agree with your concern. Then, I think this is ready now. |
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 this has taken so long, but I think we've finally iterated to the correct approach 😄
R/labels.r
Outdated
#' p + labs(title = "title") + labs(title = NULL) | ||
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(), tag = waiver()) { | ||
args <- list(..., title = title, subtitle = subtitle, caption = caption, tag = tag) | ||
if (length(args) > 0 && is.list(args[[1]]) && !is.waive(args[[1]])) args <- args[[1]] |
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.
Now that ggplot2 uses tidy eval, I think we should remove this, and in the previous line use rlang::list2()
instead of list()
. That way you can use !!!
if you do already have a list of labels.
tests/testthat/test-labels.r
Outdated
|
||
# ggtitle works in the same way as labs() | ||
expect_identical(ggtitle("my title")$title, "my title") | ||
expect_identical(ggtitle("my title", |
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.
Can you please use tidyverse style here? http://style.tidyverse.org/syntax.html#long-lines
Sorry, I'm yet to figure out how to squash the duplicated arguments properly... Will try tomorrow or after. |
@hadley
Do I get something wrong? library(ggplot2)
labs(title = "test", tag = "A")
#> $title
#> [1] "test"
#>
#> $tag
#> [1] "A"
#>
#> attr(,"class")
#> [1] "labels"
# NULL is preserved
labs(title = NULL)
#> $title
#> NULL
#>
#> attr(,"class")
#> [1] "labels"
# !!! can be used in labs()
labs(!!!list(title = "test"))
#> $title
#> [1] "test"
#>
#> attr(,"class")
#> [1] "labels"
# If the argument is duplicated, the first one will be used
labs(!!!list(title = "test"), title = "TEST")
#> $title
#> [1] "test"
#>
#> attr(,"class")
#> [1] "labels" Created on 2018-07-15 by the reprex package (v0.2.0). |
I think this looks good. Can you please now prepare a news bullet? |
Thanks, added. |
I resolved the conflict. Could someone review the news bullet? |
@batpigandme Do we have a tidyverse rule for the Oxford comma? I would probably write
and delete "explicitly". @yutannihilation Note that any further commits will cause a broken build on Travis until #2838 is resolved. You may want to wait until that time and then rebase. |
@clauswilke we are pro-Oxford-comma! |
This comment has been minimized.
This comment has been minimized.
Added the Oxford-comma, and confirmed the checks pass after merging upstream/master. |
Resolved a conflict. |
@hadley @clauswilke |
Thanks! |
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/ |
As suggested in #2663 (comment), making them as the named arguments let us organize the document easier.
Note that one difficulty I found is that
NULL
should be distinguished from missing;NULL
indicates the user's intent to remove the label. For example, the code below doesn't show the title:So, I chose to use
missing()
. But, if there is some better way to do this with rlang, please point it out.Besides, I slightly suspect
ggtitle()
may not do the right thing; the code below doesn't show the subtitle sinceggtitle()
setssubtitle
toNULL
. Is this valid for the user's intent?If this should also be changed, I'll incorporate in this PR as well.